0

I have the following structure:

Country A
|_Questionnaire 1
|_Result 1
Country B
|_Questionnaire 3
|_Result 3
Country C
|_Questionnaire 5
|_Result 5

Country ? are of CMS.folder pagetype and both questionnaire and result are of the CMS.file pagetype and contain one attachment (PDF). I am trying to access the details of the attachment (name, Guid, size) in each publication available in a folder.

I have tried the following

            .Select(m => new
            {
                Country = m.DocumentName,
                questionnaire = m.Children.WithAllData.Where(w => w.DocumentName.Contains("Questionnaire")).Select(s => s.GetValue("PDF")),
                result = m.Children.WithAllData.Where(w => w.DocumentName.Contains("Result")).Select(s => s.GetValue("PDF"))    
            })
            .ToList();

I can obrain the GUID of the attachment available in the Questionnaire file for each folder but I can not get the value of result as it seems the repetition of WithAllData twice prevents this. How could I also access the size and name of the attachment? I tried including AttachmentSize or AttachmentName but i had no success with children of the current node.

What would be the best approach to do what I am trying to do?

---------------- Update ------------------------------

As suggested, this is what I tried:

.Select(m => new
            {
                Country = m.DocumentName,
                questionnaire = GetDocs(m.Children.Where(w => w.DocumentName.Contains("Questionnaire")).FirstOrDefault()),
                result = GetDocs(m.Children.Where(w => w.DocumentName.Contains("Result")).FirstOrDefault())    
            })
            .ToList();

private PublicationSimpleDto GetDocs(TreeNode tree)
        {
            PublicationSimpleDto publication = null;
            if (tree != null)
            {
                foreach (DocumentAttachment attachment in tree.Attachments)
                {
                    publication = new PublicationSimpleDto()
                    {
                        Title = attachment.AttachmentTitle,
                        Extension = attachment.AttachmentExtension.Replace(".", "").ToUpper(),
                        AttachmentUrl = attachment.AttachmentGUID.ToString(),
                        Size = attachment.AttachmentSize
                    };
                }
            }
            return publication;
        }

However, it doesn't catch the result one it seems that I can not repeat .children several times. It was the same issue with:

questionnaire = m.Children.WithAllData.Where(w => w.DocumentName.Contains("Questionnaire")).Select(s => s.GetValue("PDF")),
                result = m.Children.WithAllData.Where(w => w.DocumentName.Contains("Result")).Select(s => s.GetValue("PDF"))

Using directly the Attachment GUID instead of the TreeNode of the page sounded easier but I am unable to do so.

Sylvain C.
  • 1,043
  • 1
  • 11
  • 27
  • The field with the page stores only the Attachmend GUID - so, you need to make another call to get the attachment info using the GUID or somehow use your current code to pass the GUID to the DocumentHelper.GetAttachment method which will return the attachment object. – jurajo Jan 09 '20 at 08:44
  • @jurajo I have edited as suggested but why can't I access the children several times. – Sylvain C. Jan 09 '20 at 18:47

1 Answers1

0

Please have a look at this Kentico docs article explaining the work with attachments.

You can try the following options:

  • Retrieve the attachment by DocumentHelper.GetAttachment
  • Access document.Attachmets property (each page type has this property)

And then you will be able to look at, for example, the size like this: attachment.AttachmentSize

Dmitry Bastron
  • 694
  • 3
  • 11
  • Thank you @dmitry-bastron, I have edited as suggested but why can't I access the children several times while applying my conditions. – Sylvain C. Jan 09 '20 at 18:46