Content Management System and E-commerce for ASP.NET
MY ACCOUNT SHOPPING CART
  phone U.S. TOLL-FREE NUMBER:
1-888-225-2767
 
 
DevNet > API and Internals > Creating link to selected page/file using Document selector
   

Creating link to selected page/file using Document selector

Document selector field stores only the GUID of the document/file which was selected, so it may be complicated to get the link for the document/file.

Applies to: Kentico CMS v3.x


Document selector field type stores the GUID of the selected page/file. When trying to use following transformation code

<a href="~/CMSPages/GetFile.aspx?nodeGuid=<%# Eval("SelectedDocument") %>">My Link</a>

this results in non-working approach for pages. The link generated above is suitable only for file processing/displaying. To
create universal method returning proper link to both files and pages it is necessary to use some custom code.
Please follow this section www.kentico.com/docs/devguide/adding_custom_functions_to_tra.htm from the Developer's guide how add custom function to your 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 AttachmentManager.GetPermanentDocUrl(nodeGUID, node.NodeAlias, CMSContext.CurrentSiteName);
                    }
                }
             }

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


Finally, please add following code to your transformation:

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

where "MyFunctions" is file name of the new class.

See also:
 
     
     
 

Technical Support

If you do not find an answer to your questions here, please contact our technical support at support@kentico.com.