SOLVED

How to get Friendly URL of a Publishing page using CSOM

Highlighted
New Contributor

Environment: SharePoint 2013 on-premise

 

I need to retrieve the friendly URL of a Publishing page.

 

I tried checking the "Hide physical URLs from search" box in the page properties, but this did not help. The page is storing the physical URL (i.e. *.aspx) of the page, and not the friendly URL.

 

I found this post that shows how to retrieve the friendly URL, but I need to accomplish this using CSOM.

https://msftplayground.com/2013/01/retrieve-the-friendly-url-of-a-publishing-page/

 

Questions - Is the GetFriendlyUrlsForListItem() method available in CSOM? If not, how can I retrieve the friendly URL of a page using CSOM?

 

I see that the GetFriendlyUrlsForListItem() method is available in SP2013 (MSDN link below)

https://msdn.microsoft.com/en-us/library/office/microsoft.sharepoint.publishing.navigation.taxonomyn...

IList<NavigationTerm> terms = TaxonomyNavigation.GetFriendlyUrlsForListItem(oListItem, false);

  

2 Replies
Best Response confirmed by Kiran Kakanur (New Contributor)
Solution

Unfortunately GetFriendlyUrlsForListItem() is not available in CSOM. I quickly wrote a sample program for you to show how you could retrieve the Friendly URL's for one of your Publishing Pages. See code below. Hope this gives you enough inspiration to write your own Production-ready code.

 

Note that this code makes use of AuthenticationManager and Extensions method(s) that are part of SharePointPnPCore* NuGet package. You could perfectly do without it...

 

class Program
    {
        static void Main(string[] args)
        {
            const string siteUrl = "";
            const string userName = "";
            const string password = "";

            using (var ctx = new AuthenticationManager().GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
            {
                var navigationTermSet = TaxonomyNavigation.GetTermSetForWeb(ctx, ctx.Web, "GlobalNavigationTaxonomyProvider", true);
                var allNavigationTerms = navigationTermSet.GetAllTerms();

                ctx.Load(allNavigationTerms, t => t.Include(
                    i => i.TargetUrl, 
                    i => i.LinkType, 
                    i => i.TaxonomyName,
                    i => i.Parent));

                ctx.ExecuteQuery();

                var navigationTermsForPage = allNavigationTerms.Where(
                    t => t.LinkType == NavigationLinkType.FriendlyUrl && 
                    t.TargetUrl.Value.Contains("/Pages/mypublishingpage.aspx")); // insert your publishing page URL here

                ctx.Web.EnsureProperty(w => w.Url); 

                foreach (var navTerm in navigationTermsForPage)
                {
                    var url = "";

                    ctx.Load(navTerm);

                    url = InsertUrlRecursive(navTerm, url);

                    Console.WriteLine($"{ctx.Web.Url}{url}");
                }
            }            
        }

        private static string InsertUrlRecursive(NavigationTerm navTerm, string url)
        {
            url = url.Insert(0, $"/{navTerm.TaxonomyName}");

            if (!navTerm.Parent.ServerObjectIsNull())
            {
                url = InsertUrlRecursive(navTerm.Parent, url);
            }

            return url;
        }
    }

 

Highlighted

Thank you so much, Paul Pascha! Greatly appreciate the quick response on this and also for the helpful code snippet.