Get TreeNode by URL

Dmitry Bastron asked on August 10, 2016 09:04

Hi community!

Is there any way to get a TreeNode object using API by relative URL (taking into account links and redirect pages)?

For example, I have a URL "~/TestPage.aspx". How can I get a TreeNode object representing that page?

Correct Answer

Dawid Jachnik answered on August 10, 2016 09:35

Hello,

I think firstly you should check the NodeAliasPath by TreeProvider, next if that the URL isn't NodeAliasPath you should check the CMS_DocumentAlias table to check to which NodeID is it connect and retrive it, for example using mentioned before TreeProvider. You can also use the DocumentHelper.GetDoccuments(...)

My example with TreeProvider:

 string aliasPath = URLHelper.RemoveExtension("/TestPage.aspx");
    TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);
    var node = tree.SelectSingleNode(SiteContext.CurrentSiteName, aliasPath, "en-US");
    if (node == null)
    {
        var aliases = DocumentAliasInfoProvider.GetDocumentAliases(aliasPath, 1);
        if (!DataHelper.DataSourceIsEmpty(aliases))
        {
            node = tree.SelectSingleNode(ValidationHelper.GetInteger(aliases.Tables[0].Rows[0]["AliasNodeID"], -1));
        }
    }
1 votesVote for this answer Unmark Correct answer

Recent Answers


Chetan Sharma answered on August 10, 2016 12:55

Yes it is possible. DocumetHelper class has Six overloaded methods and one of the methods take aliaspath as it's parameter to give you TreeNode object.

Check the documentation of the method - https://devnet.kentico.com/docs/8_2/api/html/M_CMS_DocumentEngine_DocumentHelper_GetDocument_5.htm

This should resolve your query.

Cheers, Chetan

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on August 10, 2016 12:59

Chetan, in opposite I need to get NodeAliasPath having page URL.

0 votesVote for this answer Mark as a Correct answer

Chetan Sharma answered on August 10, 2016 13:36

Dmitry, I am sorry but your question says opposite

I have a URL "~/TestPage.aspx". How can I get a TreeNode object representing that page?

The method I provided takes nodealiaspath and will give you TreeNode object.

Thanks, Chetan

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on August 10, 2016 13:43

Chetan, but "~/TestPage.aspx" is not a NodeAliasPath. It's just URL =)

And as it is just URL it can be alias or redirect like "/TestPage" or "/Test-Page"

And it seems that I have no single-line easy option to resolve URL into the TreeNode object. I need to collect all the data as David advised.

0 votesVote for this answer Mark as a Correct answer

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