Displaying link to document in transformation using custom document alias
In this article you can learn how to create custom transformation function for displaying custom links to your documents from Document Aliases. It can be used in situations where you need to display regular links to your documents in one listing web part and custom links in another listing web part.
By default, if you are calling
GetDocumentUrl() method in transformation code, this returns link to your document using
DocumentUrlPath (if exists) or
NodeAliasPath. However, if you would like to display link to your document from
custom document aliases you would need to create
custom transformation function for this purpose.
Below you can find example code which you can basically copy and paste into your transformation file. This code returns first Document alias from
CMS_ DocumentAlias table for given document:
public static string ReturnFirstCustomAlias(object nodeID, object NodeAliasPath, object DocumentUrlPath)
{
string resolvedURL = "";
if (ValidationHelper.GetInteger(nodeID, 0) != 0 )
{
DataSet ds = CMS.SettingsProvider.SqlHelperClass.ExecuteQuery("SELECT TOP 1 * FROM CMS_DocumentAlias WHERE AliasNodeID = " + nodeID.ToString(), null, CMS.IDataConnectionLibrary.QueryTypeEnum.SQLQuery );
if (!CMS.GlobalHelper.DataHelper.DataSourceIsEmpty(ds))
{
string URL = ds.Tables[0].Rows[0]["AliasURLPath"].ToString() + ds.Tables[0].Rows[0]["AliasExtensions"].ToString();
resolvedURL = CMS.GlobalHelper.UrlHelper.ResolveUrl("~" + URL);
}
else
{
resolvedURL = CMS.GlobalHelper.UrlHelper.ResolveUrl(
CMSContext.GetUrl(ValidationHelper.GetString(NodeAliasPath, null), ValidationHelper.GetString(DocumentUrlPath, null)));
}
}
return resolvedURL;
}
Then you can call the function from the transformation using following example:
MyFunctions.ReturnFirstCustomAlias(Eval("NodeID"),Eval("NodeAliasPath"), Eval("DocumentUrlPath")) %>
See also: Applies to: Kentico CMS 4.x