Portal Engine
Version 3.x > Portal Engine > File Name in Transformation View modes: 
User avatar
Guest
boodapotamus - 8/13/2008 6:06:29 AM
   
File Name in Transformation
Is there an easy way to get the actual file name of a file in a transformation?

I know it can be done using GetFileUrl, but that returns it in the format "/getattachment/GUID/DocumentName.aspx".

What I need to get is the actual file with actual file extension. Something like, "/CMSFileDirectory/audio-20080802.mp3".

User avatar
Kentico Developer
Kentico Developer
kentico_helenag - 8/13/2008 7:58:17 AM
   
RE:File Name in Transformation
Hi,

I would recommend you to create the custom function called later in the transformation that would handle this task. Please visit the http://www.kentico.com/docs/devguide/adding_custom_functions_to_tra.htm where process of creating function for transformation is described. You could inspire in this instruction:

(In Visual Studio create new Class under <your project>/App_Code and call it MyFunctions.cs. Add following code to your new class:
public static class MyFunctions
{
    public static string ReturnUrlSrc(string nodeGuidParam) 
    {
        try
        {
            if (nodeGuidParam != null && nodeGuidParam.Trim() != "")
            {
                string nodeGuidStr = ValidationHelper.GetString(nodeGuidParam, "");

                Guid nodeGUID = new Guid(nodeGuidStr);

                if (nodeGUID != null)
                {
                    int nodeId = CMS.TreeEngine.TreePathUtils.GetNodeIdByNodeGUID(nodeGUID, CMS.CMSHelper.CMSContext.CurrentSiteName);

                    if (nodeId != null)
                    {
                        CMS.TreeEngine.TreeProvider tp = new CMS.TreeEngine.TreeProvider(CMS.CMSHelper.CMSContext.CurrentUser);

                        CMS.TreeEngine.TreeNode node = tp.SelectSingleNode(nodeId);

                        return AttachmentManager.GetPermanentDocUrl(nodeGUID, node.NodeAlias, CMSContext.CurrentSiteName);
                    }
                }
            }

            return "";
        }
        catch
        {
            return "";
        }
    }
}

Also please add following code to your transformation :
<a href="<%# MyFunctions.ReturnUrlSrc(your_GUID.ToString()) %>">LINK</a>

where MyFunctions is file name of your new class.

Best Regards
Helena Grulichova

User avatar
Member
Member
boodapotamus - 8/13/2008 4:08:46 PM
   
RE:File Name in Transformation
Thanks for the quick response. I was already using custom functions, so thanks for the added code help.

It seems, that nodeGuidParam is null or not set past the "try" statement and is throwing an exception if the code is left as previously posted. I am able to get the nodeGuidParam when the exception is thrown.

I tried changing the first if statement to == null (see test code below)and it will pass the first if statement since nodeGuidParam appears to be null at that point.

I know enough C# to get me in trouble, so I may be doing something wrong. Any suggestions would be appreciated.

This is what I am uing in my transformation.
<%# MyFunctions.ReturnUrlSrc(Eval("AudioFile").ToString()) %>

//Test Code
public static string ReturnUrlSrc(string nodeGuidParam)
{
try
{
if (nodeGuidParam == null && nodeGuidParam.Trim() == "")
{
string nodeGuidStr = ValidationHelper.GetString(nodeGuidParam, "");

Guid nodeGUID = new Guid(nodeGuidStr);

if (nodeGUID != null)
{
int nodeId = CMS.TreeEngine.TreePathUtils.GetNodeIdByNodeGUID(nodeGUID, CMS.CMSHelper.CMSContext.CurrentSiteName);

if (nodeId != null)
{
CMS.TreeEngine.TreeProvider tp = new CMS.TreeEngine.TreeProvider(CMS.CMSHelper.CMSContext.CurrentUser);

CMS.TreeEngine.TreeNode node = tp.SelectSingleNode(nodeId);

return AttachmentManager.GetPermanentDocUrl(nodeGUID, node.NodeAlias, CMSContext.CurrentSiteName);
}
}
}

return "2";
}
catch
{
return "1[" + nodeGuidParam +"]";
}
}

User avatar
Kentico Developer
Kentico Developer
kentico_helenag - 8/14/2008 4:31:12 AM
   
RE:File Name in Transformation
Hi,

Could you please check if
<%# Eval("AudioFile").ToString() %>
works correctly? “nodeGuidParam” is evaluated well in my tests.

Best Regards,
Helena Grulichova

User avatar
Kentico Developer
Kentico Developer
kentico_helenag - 8/14/2008 6:12:44 AM
   
RE:File Name in Transformation
Hi again,

could I ask you, if you would like to display the URL of file in the content tree or only an attachment of other document (for example a News Teaser in the News document)?

The second case is impossible to set the URL without GUID.
The first one could be set URL without GUID but only with extension '.aspx'.

Best Regards,
Helena Grulichova

User avatar
Guest
boodapotamus - 8/14/2008 7:04:18 AM
   
RE:File Name in Transformation
Yes <%# Eval("AudioFile").ToString() %> renders correctly, that's why I was confused as to why it was not working correctly.

When using the test code above the displayed results are "1[nodeGuidParam ]", which means it's being passed to the code behind too.

The problem I am having is that on a podcast xml file the download link icon appears as a paper clip and not the headphones since the link using get attachement ends in aspx. Also, the file is displayed as Page_Name.aspx for all items.

User avatar
Kentico Developer
Kentico Developer
kentico_helenag - 8/14/2008 8:10:17 AM
   
RE:File Name in Transformation
Hi,

Unfortunately, if the file is uploaded to Kentico CMS, you may not access it with its original name with the extension. There is a possibility to store the file externally (not uploaded through CMS Desk). You could use this file's URL directly.

Best Regards,
Helena Grulichova

User avatar
Member
Member
boodapotamus - 8/14/2008 8:28:55 AM
   
RE:File Name in Transformation
Thanks for all your help. You last post put a thought into my head and worked like a charm.

Since I store my files outside the db, I was able to set the guid to string twice with the first truncated and get a link directly to the stored file.
http://www.MyDomain.com/CMSImages/<%# MyFunctions.TextLimit(Eval("AudioFile").ToString(), 2) %>/<%# Eval("AudioFile").ToString() %>.mp3

User avatar
Kentico Developer
Kentico Developer
kentico_helenag - 8/14/2008 8:43:47 AM
   
RE:File Name in Transformation
Hi,

congratulate you! It is very clever workaround :)

Best Regards,
Helena Grulichova