Attachments display in Text/XML transformation but not ASCX transformation

Greg Laycock asked on January 24, 2017 16:46

Good morning. I'm having difficulty retrieving attachments for a custom page type in a transformation. If I use a Text/XML transformation (as follows), the images display as expected.

<p><strong>Model Name:</strong> {%SKUName%}</p>
{% foreach (attachment in Documents[NodeALiasPath].AllAttachments) {
        "<img src='/getattachment/" + attachment.AttachmentGUID + "/attachment.aspx' />";
} #%}

However, due to some logic that I need to inject related to another field, I need to use an ASCX transformation. The following (pulled largely from Kentico documentation) gives no errors but displays nothing.

<%@ Register Src="~/CMSInlineControls/DocumentAttachments.ascx" TagName="DocumentAttachments" TagPrefix="cms" %>
<p><strong>Model Name:</strong> <%# Eval("SKUName") %></p> 
<cms:DocumentAttachments ID="ucDocAttachments" runat="server" PageSize="50" TransformationName="CMS.Root.Attachment" Path='<%# Eval("NodeAliasPath") %>' />

What am I missing? Both are being used in a transformation within the Kentico CMS. I'm using Kentico v10.0 and the ASPX + Portal engine. Thank you!

Correct Answer

Brenden Kehren answered on January 24, 2017 16:58

Ahh I see now. So the issue with your code is the Path='<%# Eval("NodeAliasPath") %>' property. This needs to be set differently because it isn't being evaluated properly and sending in a blank value. I have a blog post on this type of transformation.

I believe all you need to do is this in your transformation:

<script runat="server">
  protected override void OnInit(EventArgs e)
  {
      ucDocAttachments.Path = Eval<string>("NodeAliasPath");
      ucDocAttachments.ReloadData(true);
  }
</script>
0 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on January 24, 2017 16:49

Simple question, why not use the ASCX transformation? It works and doesn't add any overhead?

0 votesVote for this answer Mark as a Correct answer

Greg Laycock answered on January 24, 2017 16:53

The ASCX transformation is the one that doesn't work. I'd use the Text/XML transformation except that I need to split another field into an array based on lines, and I'm not sure there's a good way to do that in a Text/XML transformation.

0 votesVote for this answer Mark as a Correct answer

Greg Laycock answered on January 24, 2017 17:05 (last edited on January 24, 2017 17:18)

Ah... I've actually used this approach in a different transformation. I fully expected it to work here as well, but having just added that code, I'm (strangely) now getting the following error:

error CS1061: 'cmsinlinecontrols_documentattachments_ascx' does not contain a definition for 'Path' and no extension method 'Path' accepting a first argument of type 'cmsinlinecontrols_documentattachments_ascx' could be found (are you missing a using directive or an assembly reference?)

0 votesVote for this answer Mark as a Correct answer

Greg Laycock answered on January 24, 2017 17:19

I switched out the webpart reference to directly reference the module, as follows. Now the error is gone but I'm back to it displaying no data.

<%@ Register Src="~/CMSModules/Content/Controls/Attachments/DocumentAttachments/DocumentAttachments.ascx" TagName="DocumentAttachments" TagPrefix="cms" %>

0 votesVote for this answer Mark as a Correct answer

Greg Laycock answered on January 24, 2017 20:49

I figured it out. I forgot that I had switched from a standard page attachment to an attachment group, so I needed to add the AttachmentGroupGUID. Your blog post was most helpful. Thank you, Brenden!

1 votesVote for this answer Mark as a Correct answer

Chetan Sharma answered on January 30, 2017 12:35

Hi Greg,

I recently faced this problem myself where I wanted to show search results which will search for words inside document attachments. The idea was to show these attachments as links in the search results. I wrote this small method that can be put inside a transformation and method can be called by sending two parameters.

  public void Transformation(int documentID, string NodeAliasPath)
    {
        string htmlCode = null;
        // enter your attachment guid. You can find this value in the page type field.
        var fieldGUID = "";
        // get attachments for given document and field
        var attachmentsOfField = CMS.DocumentEngine.AttachmentInfoProvider.GetAttachments().WhereEquals("AttachmentGroupGUID", fieldGUID).And().WhereEquals("AttachmentDocumentID", documentID);
        foreach (var attachment in attachmentsOfField)
        {
            // get url of attachment
            string attachmentUrl = CMS.Helpers.URLHelper.GetAbsoluteUrl(CMS.DocumentEngine.AttachmentURLProvider.GetAttachmentUrl(attachment.AttachmentGUID, NodeAliasPath));
            // append html code with the url
            htmlCode += "<a href=\"" + attachmentUrl + "\">" + attachment.AttachmentName + "</a><br>";
        }
    }
1 votesVote for this answer Mark as a Correct answer

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