How to access document attachments from hierarchical transformation

300 views Asked by At

My site has this structure:

Products
    category 1
        item 1
            item 1 attachments
    category 2
        item 2
            item 2 attachments

I have successfully written a hierarchical transformation that shows the data on the page at the top level. I cannot for the life of me figure out how to access the attachments for each document though.

Anyone have any idea?

3

There are 3 answers

0
JanH On

I suppose you mean unsorted attachments you add in Properties -> Attachments section. In this case yuo can register following control in your transformation:

<%@ Register Src="~/CMSInlineControls/DocumentAttachments.ascx" TagName="DocumentAttachments" TagPrefix="cms" %>

And use it like this:

<cms:DocumentAttachments ID="ucDocAttachments" runat="server" TransformationName="cms.root.attachment" Path='<%# Eval("NodeAliasPath") %>' />
0
Enn On

If you are talking about Group attachments (this is when you have a field in your page type using the Attachments data type) then in order to access the attachments inside your transformation you need to write either a custom macro (when using Text/XML transformation) or custom transformation method. Both can be done very easily. The code itself that gets you the attachments can be like this:

public ObjectQuery<AttachmentInfo> GetAttachmentsFromField(string className, int documentID, string attachmentColumnName)
{
    // get class info       
    var classInfo = new FormInfo(DataClassInfoProvider.GetDataClassInfo(className).ClassFormDefinition);
    if (classInfo != null)
    {
        // get attachment field definition            
        var attachmentsField = classInfo.GetFormField(attachmentColumnName);
        if (attachmentsField != null)
        {
            // get attachments strored in the field by GUID
            var attachments = AttachmentInfoProvider.GetAttachments()
                .WhereEquals("3CCC6E6C-56F3-42EB-8385-979973D99C55", attachmentsField.Guid)
                .And()
                .WhereEquals("AttachmentDocumentID", documentID);

            return attachments;
        }
    }
    return null;
}

With this it is very important to take into account that this code introduces several other SQL queries against database and therefore it should be optimized by using caching appropriately.

0
Brenden Kehren On

I wrote a pretty detailed blog post on this a while back. It describes very similar attributes to Enn's answer but gives great detail on why you do specific things.