Display Attachments in Smart Search Results Transformation

Kyle G asked on April 1, 2015 15:54

I am using a Smart Search Results repeater on my site to show the latest articles. I am looking for a way to show attachments (if there are any).

I've tried using the AttachmentLightBoxGallery and the DocumentAttachments user controls, but neither works. I've also seen examples where people are setting the AttachmentGroupGUID, but there any many different ones, and you'd have to retrieve it from the DB. Is there a quick and easy way to show attachments without getting into anything custom?

<cms:AttachmentLightboxGallery ID="ucDocAttachments" runat="server" TransformationName="cms.root.attachmentLightbox" SelectedItemTransformationName="cms.root.attachmentLightboxDetail" Path='<%# GetSearchValue("NodeAliasPath") %>' />

<cms:DocumentAttachments ID="ucDocAttachments2" runat="server" TransformationName="cms.root.attachment" Path='<%# GetSearchValue("NodeAliasPath") %>' />

Recent Answers


Juraj Ondrus answered on April 2, 2015 10:05

Hi Kyle,

How are you registering the control in the transformation? Also, what form control is used for the attachments? Is it a single attachment (File upload) or Document attachments control?

For single file, like image teaser, you could use e.g. the GetFileURL method or GetImage (transformation methods). The image field must be indexed and set e.g. as content or searchable.

If you are using document attachments, you will need to create custom method, something like this, where you will use the document ID:

Attachments on FORM tab

int documentID = 988;
    string attachmentFieldColumnName = "MyAttachmentsField";
    string className = "cms.news";

    string htmlCode = null;
    // get form definition of class
    var formInfo = new FormInfo(DataClassInfoProvider.GetDataClassInfo(className).ClassFormDefinition);
    if (formInfo != null)
    {
        // get attachment field definition
        var attachmentsField = formInfo.GetFormField(attachmentFieldColumnName);
        if (attachmentsField != null)
        {
            // attachment guid
            var fieldGUID = attachmentsField.Guid;
            // get attachments for given document and field
            var attachmentsOfField = AttachmentInfoProvider.GetAttachments().Where("AttachmentGroupGUID", QueryOperator.Equals, fieldGUID).And().Where("AttachmentDocumentID", QueryOperator.Equals, documentID);
            foreach (var attachment in attachmentsOfField)
            {
                // get url of attachment
                string attachmentUrl = URLHelper.GetAbsoluteUrl(AttachmentURLProvider.GetAttachmentUrl(attachment.AttachmentGUID, DocumentContext.CurrentDocument.NodeAlias));
                // append html code with the url
                htmlCode += "<a href=\"" + attachmentUrl + "\">" + attachment.AttachmentName + "</a><br>";
            }
        }
    }

Attachments including Properties -> Attachments:

[9:59:23] sup_Richard Šůstek:   var attachmentOfDoc = AttachmentInfoProvider.GetAttachments(988, false);
    foreach (var attachment in attachmentOfDoc)
    {
        // get url of attachment
        string attachmentUrl = URLHelper.GetAbsoluteUrl(AttachmentURLProvider.GetAttachmentUrl(attachment.AttachmentGUID, DocumentContext.CurrentDocument.NodeAlias));
        // append html code with the url
        htmlCode1 += "<a href=\"" + attachmentUrl + "\">" + attachment.AttachmentName + "</a><br>";
    }
0 votesVote for this answer Mark as a Correct answer

Kyle G answered on April 2, 2015 14:46

I have a field on my Page Types with a Data Type of "Attachments" and the form control is "Attachments control". The user adds the documents via the Form view.

0 votesVote for this answer Mark as a Correct answer

Kyle G answered on April 2, 2015 15:30

I would be happy with something that just checks to see if there are attachments and returns a yes/no. There are a lot of people having issues with attachments in transformations/repeaters. It should be a little easier I think.

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on April 3, 2015 10:14

Hi,

In this case you can use the IfEmpty transformation method and compare the values. Or create a simple custom method that will check the field for a null value.

0 votesVote for this answer Mark as a Correct answer

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