Absence/presence of picture file

Sylvain C. asked on August 1, 2019 23:55

Hi,

I have a page type which an optional File field for storing picture or not. My DTO is as follow:

 public class ContentItemDto
    {
        public int NodeId { get; set; }
        public Guid NodeGuid { get; set; }
        public string NodeAlias { get; set; }
        public string Title { get; set; }
        public string Text { get; set; }
        public DocumentAttachment Picture { get; set; }
    }

and my code in my repo:

private Func<CMS.DocumentEngine.Types.ContentItem, ContentItemDto> ContentItemDtoSelect => item => new ContentItemDto()
{          
    Picture = item.Fields.Picture,       
    Title = item.Title,
    Text = item.Content,
    NodeAlias = item.NodeAlias,
    NodeGuid = item.NodeGUID,
    NodeId = item.NodeID,
};

This is where my code is complaining as it is expecting a picture which does not not exist.

Is it possible to declare an optional DocumentAttachment? What would be a workaround in this case?

Sylvain

Recent Answers


Zach Perry answered on August 2, 2019 17:30

Did you try making the document attachment nullable?

public class ContentItemDto
    {
        public int NodeId { get; set; }
        public Guid NodeGuid { get; set; }
        public string NodeAlias { get; set; }
        public string Title { get; set; }
        public string Text { get; set; }
        public DocumentAttachment? Picture { get; set; }
    }
0 votesVote for this answer Mark as a Correct answer

Sylvain C. answered on August 2, 2019 17:42

Thank you Zach,

Yes I tried to set the DocumentAttachment as nullable but gets the following error: The feature 'nullable reference types' is currently in Preview and *unsupported*.

I don't know what to do.

0 votesVote for this answer Mark as a Correct answer

Zach Perry answered on August 2, 2019 17:57

Are you using C# 8?

0 votesVote for this answer Mark as a Correct answer

Sylvain C. answered on August 2, 2019 18:01

I am targeting .Net Framework 4.6.1 so I am probably using C# 6.

0 votesVote for this answer Mark as a Correct answer

Zach Perry answered on August 2, 2019 18:19

Looks like documentattachment is already nullable. You sure it's breaking there and not in the view for example using the class? Anywhere you are calling ContentItemDto.Picture needs to check if it is null.

0 votesVote for this answer Mark as a Correct answer

Sylvain C. answered on August 2, 2019 18:39 (last edited on August 2, 2019 18:45)

It is really in my repository that the error is occuring: CMS.DocumentEngine.Types.IATTC.ContentItem.ContentItemFields.Picture.get returned null.

I tried the follwoing and it works:

private Func<CMS.DocumentEngine.Types.ContentItem, ContentItemDto> ContentItemDtoSelect => item => new ContentItemDto()
        {          
            Picture = item.Picture != Guid.Empty ? item.Fields.Picture : null,
            PictureURL =item.Picture != Guid.Empty ? item.Fields.Picture.AttachmentUrl : "",
            Title = item.Title,
            Text = item.Content,
            NodeAlias = item.NodeAlias,
            NodeGuid = item.NodeGUID,
            NodeId = item.NodeID,
        };

The field need to be tested before trying to get its value.

Thank you very much for your time

0 votesVote for this answer Mark as a Correct answer

Zach Perry answered on August 2, 2019 18:44

Does it work if you return a document that does have an attachment?

Is Item.Picture returning a DocumentAttachment or just a Guid?

0 votesVote for this answer Mark as a Correct answer

Sylvain C. answered on August 2, 2019 18:47

Thank You Zach: The following works:

Private Func<CMS.DocumentEngine.Types.ContentItem, ContentItemDto> ContentItemDtoSelect => item => new ContentItemDto()
        {          
            Picture = item.Picture != Guid.Empty ? item.Fields.Picture : null,
            PictureURL =item.Picture != Guid.Empty ? item.Fields.Picture.AttachmentUrl : "",
            Title = item.Title,
            Text = item.Content,
            NodeAlias = item.NodeAlias,
            NodeGuid = item.NodeGUID,
            NodeId = item.NodeID,
        };

The item.Picture is a Guid coming from a File type.

Thank you again for your time.

0 votesVote for this answer Mark as a Correct answer

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