get form attachments by attachmentgroupguid in an xml transformation

Dennis Hulsmans asked on May 17, 2016 18:10

Hi

I've got 2 separate fields in a page type which can hold attachments (1 field for technical documents, and another one for images).

Now I want to render hyperlinks to these files, so they can be downloaded. I've got something like this at the moment, but performance wise it's not OK and I don't want to check the attachment extension

{% 
     docAttachments = Documents[NodeALiasPath].AllAttachments;
     foreach (attachment in docAttachments ) {
       if (attachment.AttachmentExtension.Trim(".") != "png") {  
         "<a href='/getattachment/" + attachment.AttachmentGUID + "/" + attachment.AttachmentName + "' target='_blank'>" 
           + "<span class='icon-" + attachment.AttachmentExtension.Trim(".") + "'></span>"
           + attachment.AttachmentName + 
         "</a>";
       }
     } 
   #%}

I also need to do this twice, because I have 2 separate fields, so I don't want to get Documents[NodeALiasPath].AllAttachments twice. I can put this at the top of my transformation, but then I guess I'll have to get the AttachmentGroupGUID in some way (but I don't know how).

Another solution could be a custom transformation?

Any advice would be appreciated.

Correct Answer

Brenden Kehren answered on May 17, 2016 18:31

Any reason you need a text/xml transformation? I have some instructions here how to do this with an ascx transformation.

2 votesVote for this answer Unmark Correct answer

Recent Answers


Roman Hutnyk answered on May 17, 2016 18:30

What about changing transformation to ASPX and using GetFileUrl method?

2 votesVote for this answer Mark as a Correct answer

Dennis Hulsmans answered on May 18, 2016 12:33 (last edited on May 18, 2016 14:01)

Hi

I'm not that familiar with ascx transformations. I'm trying to convert following transformation to ascx, but I'm constantly fighting errors. I guess I'm running in circles at the moment :)

I'm trying to render all form fields, including their attachments (2 fields contain attachments)

<%# 
  docAttachments = Documents[NodeALiasPath].AllAttachments;
  return;
%>

<%# if (docAttachments.Any()) { %>
  <div class="product_detail_content">
    <a class="product_detail_content-link" data-toggle="collapse" href="#product-content-2" aria-expanded="false" aria-controls="collapseExample">
      <h2>{$ REN.ProductTechnicalFiles $}</h2>
      <span class="fa fa-caret-down"></span>
    </a>
    <div class="collapsed  in product_detail_content-text" id="product-content-2">
       <div class="product-detail-tech">
         <%# 
           foreach (attachment in docAttachments ) {
             if (attachment.AttachmentExtension.Trim(".") != "png") {  
               "<a class='cta__button' href='/getattachment/" + attachment.AttachmentGUID + "/" + attachment.AttachmentName + "' target='_blank'>" 
                 + "<span class='icon-" + attachment.AttachmentExtension.Trim(".") + "'></span>"
                 + attachment.AttachmentName + 
               "</a>";
             }
           } 
         #%>
       </div>
    </div>
  </div>

  <div class="product_detail_content">
    <a class="product_detail_content-link" data-toggle="collapse" href="#product-content-3" aria-expanded="false" aria-controls="collapseExample">
      <h2>{$ REN.ProductTechnicalDrawings $}</h2>
      <span class="fa fa-caret-down"></span>
    </a>
    <div class="collapsed  in product_detail_content-text" id="product-content-3">
       <div class="product-detail-drawing">
         <%# 
           foreach (attachment in docAttachments ) {
             if (attachment.AttachmentExtension.Trim(".") == "png") {  
               "<a class='cta__button' href='/getattachment/" + attachment.AttachmentGUID + "/" + attachment.AttachmentName + "' target='_blank'>" 
                 + "<span class='icon-" + attachment.AttachmentExtension.Trim(".") + "'></span>"
                 + attachment.AttachmentName + 
               "</a>";
             }
           } 
         #%>
       </div>
    </div>
  </div>
<%# } %>

How do I declare variables known throughout the current transformation;
example: docAttachments = Documents[NodeALiasPath].AllAttachments;

error: [TempITemplate.Template]: http://server/CMSVirtualFiles/Transformations/=vg=71bc1b86-7d88-499a-a38c-6d4288d36c2c/REN.ProductDetail/ProductDetailsLeft.ascx(9): error CS0103: The name 'Documents' does not exist in the current context

for strings or translations I've found an ascx solution. But now I need to get the attachments from a specific field

<%
  string subTitleContent1 = Localize("REN.ProductAdvantages");
%>

<h2><%= subTitleContent1 %></h2>

So I guess there has to be another way to iterate through all the attachments

1 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on May 18, 2016 14:02

"Is there no descent tutorial for this? "

Did you not read the post I left about the exact thing you are looking for BUT in ascx format? It's pretty simple, change the type to ASCX and copy and paste the code then make a few modifications to the field names.

Read this post

2 votesVote for this answer Mark as a Correct answer

Dennis Hulsmans answered on May 18, 2016 17:23 (last edited on May 18, 2016 18:03)

Hi Brenden

Yes I've read it, but I expected that a functionality like that would be a bit more "out-of-the-box" :) .
I've got it working now!

Thanks Brenden for your patience and guidance :). I still need to get my mind around those transformations. Especially when you're doing stuff like this INSIDE a transformation.

Do you have any advice on how to debug or know what to get if you're using this approach?

1 votesVote for this answer Mark as a Correct answer

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