Get attachment URLs for a PARTICULAR fieldName for a Page Type

Shweta S asked on April 17, 2017 23:11

Hi,

I am able to get all attachments for a page type but I am not able to get only for a fieldName and not all the attachments.

Brenden's solution works : https://devnet.kentico.com/questions/attachments-in-transformation

But I am curious if there is an easier way to just filter by fieldName or something like that for below solution:

// Creates a new instance of the Tree provider
TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

// Gets a page
TreeNode page = tree.SelectNodes()
    .Path("/Articles")
    .OnCurrentSite()
    .Culture("en-us")
    .FirstObject;

if (page != null)
{
    // Iterates over all attachments of the page
    foreach (BaseInfo attachment in page.AllAttachments)
    {
        AttachmentInfo publishedAttachment = attachment as AttachmentInfo;
        if (publishedAttachment != null)
        {
            // Perform any action with the attachment object (AttachmentInfo)
        }
    }
}

Correct Answer

Peter Mogilnitski answered on April 18, 2017 21:24

When you use field attachment - Kentico saves Attachment GUID to the db as a value for this field . For example if your field called MenuItemTeaserImage and you do tree.GetValue("MenuItemTeaserImage") you should get GUID of the attachment

   ...
   AttachmentInfo publishedAttachment = attachment as AttachmentInfo;
   if (publishedAttachment != null)
        {
           if (tree.GetValue(AttachmentFieldName) == publishedAttachment.GUID) {
           // you found fieldName  attachment

         }
    }
0 votesVote for this answer Unmark Correct answer

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