How to display link to a file or document selected with Document Selector

   —   
This article shows how to display a user friendly link without GIUD to the document or file which was uploaded to some document as an attachment (e.g. on the Form tab using file upload form control). The Document Selector field type is storing only the GUID of the selected item.
If you want to display the alias path according to the given GUID number it is recommended to create the custom function called later in the transformation that would handle this task. Please see "Adding custom functions to transformations" article, where process of creating function for transformation is described. In your custom function you would call the "CMS.TreeEngine.TreeProvider.SelectSingleNode(Guid, String cultureCode, String siteName)" where you pass your GUID value, the culture code as the "CMSContext.CurrentPageInfo.DocumentCulture" and the site name as the "CMSContext.CurrentSite.SiteName". Returned TreeNode object should contain all necessary information that could be returned to and displayed by the transformation.

In Visual Studio create new Class under <your project>/App_Code and call it e.g. 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 CMS.CMSHelper.CMSContext.GetUrl(node.NodeAliasPath, node.DocumentUrlPath, CMSContext.CurrentSiteName); } } } return ""; } catch { return ""; } } }

Also please add following code to your transformation :

<a href="<%# MyFunctions.ReturnUrlSrc(Eval("DocumentSelectorField ").ToString()) %>">LINK</a>

where MyFunctions is file name of your new class.


UPDATE FOR HIGHER VERSIONS:

You can use the built-in transformation function: GetDocumentUrl(object nodeGuidColumn, object nodeAlias).

Example:

<%# GetDocumentUrl("ProductPageLink", Eval("NodeAlias")) %>


See also:


Applies to: Kentico CMS 3.x and later
Share this article on   LinkedIn

Juraj Ondrus

Hi, I am the Technical support leader at Kentico. I'm here to help you use Kentico and get as much as possible out of it.