Get NodeAliasPath of another page from code behind.

Sam Riveros asked on July 5, 2018 20:06

Hello there,

I looked everywhere in the documentation and Googled for hours on this... I must be blind or something...

I know how to get the NodeAliasPath of the current document and the parent document. But how to I get the NodeAliasPath of another document?

// Ways to get current page
string nodeAliasPath = CurrentDocument.GetValue("NodeAliasPath").ToString();
string nodeAliasPath = CMS.DocumentEngine.DocumentContext.CurrentAliasPath;
// Ways to get parent page
string parentNodeAliasPath = CurrentDocument.Parent.GetValue("NodeAliasPath").ToString();

When a Page Type is created, a primary key is automatically added. I looked at the database table which was created and did not find a column where the NodeAliasPath is stored.

I did find another table called CMS_Document and CMS_DocumentAlias, but did not find a foreign key which I could use to make the association. Is there a way to get the NodeAliasPath by the primary key?

I did see DocumentID, DocumentNodeID, and DocumentNamePath. But those IDs don't match the primary key of my page type. And the DocumentNamePath is not well formatted. Is there another table I should be looking at?

Thanks, Sam

Recent Answers


Peter Mogilnitski answered on July 5, 2018 20:24 (last edited on July 5, 2018 22:06)

NodeAliasPath comes from cms_tree table, normally you would use the view view_cms_tree_joined. To join your page type you use cms_document.DocumentForeignKeyValue = yourtypetable.primarykeyfield. Take look at the documentation.

Let say your custom document type is article and its table is CONTENT_Article, so the query would be:

select  nodeAliasPath, DocumentCulture, *  
from View_CMS_Tree_Joined 
join CONTENT_Article on DocumentForeignKeyValue  = ArticleID and ClassName = 'CMS.Article'
where NodeAliasPath like '/Services/%' and NodeSiteID =3

or via documenthelper something like

var documents = DocumentHelper.GetDocuments("CMS.Article")
                              .OnSite("CorporateSite")
                              .Path("/Services/%");
0 votesVote for this answer Mark as a Correct answer

Chris Bass answered on July 5, 2018 20:34

CurrentDocument.Parent is a TreeNode, so it has all the standard TreeNode properties, including NodeAliasPath.

Depending on your Kentico version, if you're needing some other page, I recommend DocumentHelper. DocumentHelper.GetDocuments() has a large set of methods for finding the pages you want - Column-level WhereEquals/etc filters, Type filters, Published(), etc. You're basically building a SQL query using the ObjectQuery syntax. Once you do that, use .TypedResult to get the InfoDataSet of TreeNodes you want. If you only want the NodeAliasPath, add .Columns("NodeAliasPath"), otherwise specify all the columns you want (or don't use a Columns() filter)

As far as DocumentNamePath, that's the tree-like list of Page Names of the parents, NodeAliasPath is the list of their individual page aliases, which are slightly different.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on July 6, 2018 01:49

This simple API call should get what you need. This is assuming the variable "documentId" is the document ID of the page in question and you already have it. If you don't hvae the document's ID then you'd need some other identifying factor(s) to get it narrowed down to a single record.

TreeProvider tree = new TreeProvider();
string path = string.Empty;
var node = DocumentHelper.GetDocument(documentId, tree);
if (node != null)
{
    path = node.NodeAliasPath;
}
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.