how to handle nulls with attachmentUrlRetriever

lawrence whittemore asked on June 17, 2021 20:55

I have something like this

public static DepartmentDocumentsViewModel GetViewModel(DepartmentDocument departmentDocument, IPageUrlRetriever pageUrlRetriever, IPageAttachmentUrlRetriever attachmentUrlRetriever)
    {
        return new DepartmentDocumentsViewModel
        {
            DepartmentDocumentID = departmentDocument.DepartmentDocumentID,
            Title = departmentDocument.Title,
            Type = departmentDocument.Type,
            File = departmentDocument.File,              
            AttachmentPath = attachmentUrlRetriever.Retrieve(departmentDocument.Fields.File).RelativePath,
            Url = pageUrlRetriever.Retrieve(departmentDocument).RelativePath
        };
    }

How would I handle the attachmentUrlRetriever for a null field?

Correct Answer

lawrence whittemore answered on June 22, 2021 15:08

this worked for me

AttachmentPath = departmentDocument.Fields.File == null ? null : attachmentUrlRetriever.Retrieve(departmentDocument.Fields.File).RelativePath,
0 votesVote for this answer Unmark Correct answer

Recent Answers


lawrence whittemore answered on June 17, 2021 21:46

I am trying to do it on the cshtml page but am getting an error

Cannot implicitly convert type 'System.Guid' to 'CMS.DocumentEngine.IAttachment'
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.