title: Access attachments in children of a page - Kentico
body: <p>I have the following structure:</p>
  
  <p>Country A<br/>
   |_Questionnaire 1<br>
   |_Result 1<br>
  Country B<br/>
   |_Questionnaire  3<br>
   |_Result 3<br>
  Country C<br/>
  |_Questionnaire 5<br>
  |_Result 5<br></p>
  
  <p>Country ? are of <strong>CMS.folder</strong> pagetype and both questionnaire and result are of the <strong>CMS.file</strong> 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.</p>
  
  <p>I have tried the following</p>
  
  <pre><code>            .Select(m =&gt; new
              {
                  Country = m.DocumentName,
                  questionnaire = m.Children.WithAllData.Where(w =&gt; w.DocumentName.Contains("Questionnaire")).Select(s =&gt; s.GetValue("PDF")),
                  result = m.Children.WithAllData.Where(w =&gt; w.DocumentName.Contains("Result")).Select(s =&gt; s.GetValue("PDF"))    
              })
              .ToList();
  </code></pre>
  
  <p>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 <code>AttachmentSize or AttachmentName</code> but i had no success with children of the current node.</p>
  
  <p>What would be the best approach to do what I am trying to do?</p>
  
  <p>---------------- Update ------------------------------</p>
  
  <p>As suggested, this is what I tried:</p>
  
  <pre><code>.Select(m =&gt; new
              {
                  Country = m.DocumentName,
                  questionnaire = GetDocs(m.Children.Where(w =&gt; w.DocumentName.Contains("Questionnaire")).FirstOrDefault()),
                  result = GetDocs(m.Children.Where(w =&gt; 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;
          }
  </code></pre>
  
  <p>However, it doesn't catch the result one it seems that I can not repeat <code>.children</code> several times. It was the same issue with:</p>
  
  <pre><code>questionnaire = m.Children.WithAllData.Where(w =&gt; w.DocumentName.Contains("Questionnaire")).Select(s =&gt; s.GetValue("PDF")),
                  result = m.Children.WithAllData.Where(w =&gt; w.DocumentName.Contains("Result")).Select(s =&gt; s.GetValue("PDF"))
  </code></pre>
  
  <p>Using directly the Attachment GUID instead of the TreeNode of the page sounded easier but I am unable to do so.</p>