Get document by document url

GTEK IT DEV Team asked on March 2, 2020 07:22

Now i have an url. And i want to get the document(TreeNode) according to that url. That url can be NodeAliasPath, Page aliases or Page aliases?

Thanks

Recent Answers


Juraj Ondrus answered on March 2, 2020 08:56

What is the use case? Where are you getting the URL from? I would recommend using the document query API as shown in the API examples to work with pages.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on March 2, 2020 16:20

Along with Juraj's API Examples documentation a simple query should work:

var docs = DocumentHelper.GetDocuments("CMS.MenuItem").Where("NodeAliasPath LIKE '/your/path/here/%'");

Of course you'll want to optimize this to use caching, set column names, etc.

0 votesVote for this answer Mark as a Correct answer

Eric Dugre answered on March 4, 2020 19:46

If you need to find a page based on a URL, you could try PageInfoProvider.GetPageInfoForUrl()

1 votesVote for this answer Mark as a Correct answer

Ryan Nguyen answered on March 6, 2020 08:24 (last edited on March 6, 2020 08:29)

You may try get NodeID from DocumentAlias then get node by NodeID

TreeNode GetNodeFromAlias(string aliasPath)
{
    TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);
    TreeNode page = null;
    page = tree.SelectNodes()
        .Path(nodePath, PathTypeEnum.Explicit)
        .OnCurrentSite()
        .FirstOrDefault();

    if(page != null){
        return page;
    }
    // try get NodeID from DocumentAliases
    var alias = DocumentAliasInfoProvider.GetDocumentAliases()
        .WhereEquals("AliasURLPath", aliasPath)
        .FirstOrDefault();
    if(alias == null)
    {
        return null;
    }
    return tree.SelectNodes()
        .WhereEquals("NodeID", alias.AliasNodeID)
        .OnCurrentSite()
        .FirstOrDefault();
}
2 votesVote for this answer Mark as a Correct answer

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