Method 1: Using Page Url Path Info Provider (Recommended)
You can query the Page Url Path Info Provider to find the mapping for the URL path, which will give you the Node ID. From there, you can easily fetch the actual document.
C#
using CMS.DocumentEngine;
using CMS.Webpages;
// 1. Clean the relative URL path (e.g., "/my-document")
string urlPath = "/my-document";
// 2. Get the URL path info matching the slug
var pathInfo = PageUrlPathInfoProvider.GetPageUrlPaths()
.WhereEquals("PageUrlPathUrlPath", urlPath.TrimStart('/')) // Kentico stores paths relative without leading slash typically, or use standard cleaning
.TopN(1)
.FirstOrDefault();
if (pathInfo != null)
{
// 3. Retrieve the page using the NodeID found
TreeNode page = new DocumentQuery()
.WhereID("NodeID", pathInfo.PageUrlPathNodeID)
.LatestVersion(false) // Set to true if you need the edited version in Kentico admin
.Published(true)
.FirstOrDefault();
if (page != null)
{
// You now have your document/page object
}
}
Method 2: Direct DocumentQuery Extension
If you prefer a more direct approach, you can join your document query with the URL path table (CMS_PageUrlPath) using standard Kentico DataQuery APIs:
C#
using CMS.DocumentEngine;
using System.Linq;
string urlPath = "my-document"; // Path without leading slash
var page = new DocumentQuery()
.Source(s => s.Join("CMS_PageUrlPath", "NodeID", "PageUrlPathNodeID"))
.WhereEquals("PageUrlPathUrlPath", urlPath)
.LatestVersion(false)
.Published(true)
.FirstOrDefault();
Key Considerations:
Sites: If you are running a multi-site environment, make sure to add .OnSite("YourSiteName") to your query or filter the Page Url Path Site ID to avoid conflicts if the same slug exists on multiple sites.
Culture: If the slug is culture-specific, you may also want to filter by or ensure the Document Culture matches your requirements.
ALSO VISIT THIS: https://altrixcalculator.com/