Portal Engine Questions on portal engine and web parts.
Version 6.x > Portal Engine > Attachment transformation View modes: 
User avatar
Member
Member
Snarrak - 1/11/2012 2:19:07 PM
   
Attachment transformation
Hi there,

I want to display the description and title of an attachment.
I'm wondering why this won't work..


<%@ Register TagPrefix="cc1" Namespace="CMS.GlobalHelper" Assembly="CMS.GlobalHelper" %>
<div id="divLink">
<a target="_blank" href="<%# GetAbsoluteUrl(GetAttachmentUrl(Eval("AttachmentName"), Eval("NodeAliasPath")), Eval<int>("AttachmentSiteID")) %>">
<%# Eval("AttachmentName",true) %>
<%# Eval("AttachmentDescription",true) %>
</a>
</div>

User avatar
Member
Member
kentico_michal - 1/16/2012 2:57:42 AM
   
RE:Attachment transformation
Hello,

This is standard behavior, as the data related to the attachment file are not part of the document and they are not retrieved when using standard listing web parts and controls. You will need to create a custom transformation method, access the attachment using API in the code of the transformation method based on some identification passed to this method and return a value that you need.

Best regards,
Michal Legen

User avatar
Member
Member
Snarrak - 1/16/2012 5:58:25 AM
   
RE:Attachment transformation
I'm not sure how to write my code in transformations, should it be something like this:

<%# GetAttachmentTitle(Eval("AttachmentGUID", "MySiteName")); %>
or
<%# GetAttachmentInfo(Eval("AttachmentGUID", "MySiteName")).AttachmentTitle; %>

Also I was trying to put the code in a custom function. Normally there is a class called MyFunctions.cs in the App_Code folder. But I'm working on a web application project. Where should I put the class in order for Kentico to find my custom functions from the transformations?

User avatar
Member
Member
kentico_michal - 1/17/2012 1:36:05 AM
   
RE:Attachment transformation
Hello,

Open the web project in Visual Studio. Create a new folder under the App_Code folder or Old_App_Code if you have installed the project as a web application and call it as your site code name. Right-click the folder and choose Add New Item. Choose to add a new Class and call it MyFunctions.cs

More information: Adding custom functions to transformations.

Best regards,
Michal Legen

User avatar
Member
Member
Snarrak - 1/17/2012 2:43:26 AM
   
RE:Attachment transformation
I put it in the main-folder and it works. Still have to try if it works on the server as well, but otherwise I can try putting it on the Old_App_Code-folder, thanks for that! And thanks for the link.

I got it working with the following code:
Transformation:

<%@ Register TagPrefix="cc1" Namespace="CMS.GlobalHelper" Assembly="CMS.GlobalHelper" %>
<div id="divAttachmentCSS">
<a target="_blank" href="<%# GetAbsoluteUrl(GetAttachmentUrl(Eval("AttachmentName"), Eval("NodeAliasPath")), Eval<int>("AttachmentSiteID")) %>">
<%# CMSApp.MyFunctions.GetAttachmentTitle(Eval("AttachmentGuid").ToString(),Eval("AttachmentName").ToString()) %>
</a>
</div>


MyFunctions class, GetAttachmentTitle:

public static string GetAttachmentTitle(string attachmentGUID, string attachmentName)
{
try
{
AttachmentManager am = new AttachmentManager();
AttachmentInfo info = am.GetAttachmentInfo(attachmentGUID, "MySiteName");
if (info.AttachmentTitle == "")
{
return attachmentName;
}
return info.AttachmentTitle;
}
catch (Exception)
{
return attachmentName;
}
}

User avatar
Member
Member
kentico_michal - 1/17/2012 3:05:43 AM
   
RE:Attachment transformation
Hello,

I am glad I could help and thank you for sharing your solution.

Best regards,
Michal Legen

User avatar
Member
Member
Andy - 1/17/2012 10:23:52 AM
   
RE:Attachment transformation
Thanks for posting this. Snarrak, in your original code that was not working, were you using an article list webpart? I presume it was not the document attachments webpart, since, in that, <%# Eval("AttachmentName") #> resolve fine.
Thanks

User avatar
Member
Member
Snarrak - 1/17/2012 11:04:13 AM
   
RE:Attachment transformation
Hi Andy,

There's a difference between AttachmentTitle and AttachmentName.
The AttachmentName is something like example-of-my-attachment.pdf and the title could be anything, the user can add this through the CMSDesk.

What my code does, is getting the title, when filled in. If not filled in by the user the example-of-my-attachment.pdf is shown on the site.

AttachmentTitle needs to be pulled out of the database, attachmentName is easy to set in the transformation.

User avatar
Member
Member
Andy - 1/17/2012 12:36:02 PM
   
RE:Attachment transformation
OK, sorry, I used a bad example.
To rephrase: in a document attachments webpart, for me, eval("AttachmentTitle") and eval("AttachmentDescription") both resolve correctly without a custom function.

That is why I was assuming that you were using an article list web part (or something similar to that).

It makes sense that in an attachments webpart, the container.dataitem has all the attachment properties hooked up, but when you are using something like an article list web part, that the set of data passed back would not have all the lesser-used properties populated ... hence why you need to use your custom function. This was really helpful, and actually I have a further observation/question on this, but before I ask it, I wanted to clarify the context (which web part) in which you were using your transformation originally.

Thanks again.


User avatar
Member
Member
Snarrak - 1/17/2012 2:02:02 PM
   
RE:Attachment transformation
I guess I tried to use the "AttachmentTitle" on a webpart that wasn't the DocumentAttachments. At first I had another webpart, I'm not sure which it was, sorry.. But the "AttachmentTitle" wasn't possible in that one.

In the documentAttachment-webpart, this code is enough:


<%@ Register TagPrefix="cc1" Namespace="CMS.GlobalHelper" Assembly="CMS.GlobalHelper" %>
<div id="divAttachmentCSS">
<a target="_blank" href="<%# GetAbsoluteUrl(GetAttachmentUrl(Eval("AttachmentName"), Eval("NodeAliasPath")), Eval<int>("AttachmentSiteID")) %>">
<%# IfEmpty(Eval("AttachmentTitle"), Eval("AttachmentName"), Eval("AttachmentTitle")) %>
</a>
</div>



Thanks for that :)
The result is the same but I think this should help my performance a little bit.