Attachments in transformation

Marc-André Morin asked on January 16, 2019 20:19

Hello, I'm new to Kentico and i'm struggling so much with the attachments in transformation.

I've check a lot of blog post about it and a lot of question in this forum but I can't quite make this works.

So I got an attachment field named "Image" in my "page type". In my transformation I want to retrieve the url of the attachment. Here's my code :

Thanks in advance!

<div>
<img src='<%# GetAbsoluteUrl(GetAttachmentUrl("Image", Eval("NodeAliasPath"))) %>' />
  <div class="text">
    <h2><%# Eval("Title") %></h2>
    <%# Eval("Content") %>
  </div>
</div>

Recent Answers


Ron Truex answered on January 16, 2019 22:56 (last edited on January 17, 2019 04:39)

Have you checked out this page https://docs.kentico.com/k11/developing-websites/loading-and-displaying-data-on-websites/writing-transformations/reference-transformation-methods

To me I would think your src should be GetAbsoluteUrl(GetAttachmentUrl(Eval<string>("Image"), Eval<string>("NodeAliasPath")))

Also you can use double quotes for your source and do not need single quotes.

0 votesVote for this answer Mark as a Correct answer

Zach Perry answered on January 17, 2019 04:07

Have you checked what value is stored in the Image field?

If it is the attachment file name, you can use: <% GetAttachmentUrl(Eval("Image"), Eval("NodeAliasPath"))) %>

If it is a GUID, you can use: <% GetAttachmentUrlByGUID(Eval("Image"), Eval("NodeAliasPath"))) %>

0 votesVote for this answer Mark as a Correct answer

Marc-André Morin answered on January 17, 2019 19:56 (last edited on January 17, 2019 19:59)

My Image field seems to be empty when I do <%# Eval("Image") %>.

It was REALLY complicated, but I finalliy get this to work with another Transformation and the code below :

<%@ Register Src="~/CMSModules/Content/Controls/Attachments/DocumentAttachments/DocumentAttachments.ascx" TagName="DocumentAttachments" TagPrefix="cms" %>
<script runat="server">
  protected override void OnInit(EventArgs e)
  {
    CMS.DataEngine.DataClassInfo dci = CMS.DataEngine.DataClassInfoProvider.GetDataClassInfo(Eval("ClassName").ToString(), true);
    if (dci != null)
    {
      CMS.FormEngine.FormInfo fi = new CMS.FormEngine.FormInfo(dci.ClassFormDefinition);
      // field name we added in the page type
      CMS.FormEngine.FormFieldInfo ffi = fi.GetFormField("Image");  
      Guid AttachmentGroupGUID = ffi.Guid;
      ucDocAttachments.Path = Eval<string>("NodeAliasPath");
      ucDocAttachments.AttachmentGroupGUID = AttachmentGroupGUID;
      ucDocAttachments.TransformationName = "CMS.Root.DisplayImage";
      ucDocAttachments.ReloadData(true);
    }
  }
</script>
<%# GetAttachmentIcon("AttachmentGUID") %>
<div>
  <div class="img"><cms:DocumentAttachments ID="ucDocAttachments" runat="server" /></div>
  <div class="text">
    <h2><%# Eval("Title") %></h2>
    <%# Eval("Content") %>
  </div>
</div>
0 votesVote for this answer Mark as a Correct answer

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