Display Page URL

Kentico 13 display page url

To display the URL of a specific page in Kentico Xperience, you can retrieve the NodeAliasPath of the page and construct its URL based on your site’s configuration.

Replace “/YourPageNodeAliasPath” with the actual NodeAliasPath of the page whose URL you want to display. Also, replace “YourSiteName” with the name of your Kentico Xperience site where the page resides.

This code retrieves the specified page using the DocumentHelper, then fetches the site information using SiteInfoProvider. Finally, it constructs the page URL by combining the site’s presentation URL and the NodeAliasPath of the page.

Ensure you have the necessary Kentico Xperience references and proper setup in your project to use the Kentico API and TreeNode class.

Here’s an example C# code snippet that demonstrates how to get the URL of a page:

using CMS.DocumentEngine;
using CMS.SiteProvider;
using System;

class Program
{
    static void Main(string[] args)
    {
        // Replace 'NodeAliasPath' with the NodeAliasPath of your page
        string nodeAliasPath = "/YourPageNodeAliasPath";

        // Replace 'YourSiteName' with the name of your site
        string siteName = "YourSiteName";

        // Get the specific page by its NodeAliasPath
        TreeNode page = DocumentHelper.GetDocuments()
            .Path(nodeAliasPath)
            .OnSite(siteName)
            .FirstOrDefault();

        if (page != null)
        {
            // Get the site object
            SiteInfo site = SiteInfoProvider.GetSiteInfo(siteName);

            // Construct the full URL of the page
            string pageUrl = $"{site.SitePresentationURL.TrimEnd('/')}/{page.NodeAliasPath.TrimStart('/')}";
            
            Console.WriteLine($"URL for the page: {pageUrl}");
        }
        else
        {
            Console.WriteLine($"Page with NodeAliasPath '{nodeAliasPath}' not found.");
        }
    }
}