1 minute read

One of my first projects with SharePoint 2013 is building a SharePoint website. For the website we had to build a functionality that display’s the URL of specific pages.

For the website we are using Managed Navigation. Displaying URLs meant we wanted to display the friendly URLS. After searching for a while with ILSpy and looking at some MSDN articles:

I found out that the friendly URL can be retrieved by first getting a list of NavigationTerm items from a SharePoint list item. When you have a NavigationTerm you can retrieve the display URL of that term using the method “GetResolvedDisplayUrl”.

In short the code to retrieve the friendly URLs will look something like this.

public List<string> GetPagesUrls() {
    
    //list for saving the urls
    List<string> retVal = new List<string>();

    //current web
    SPWeb web = SPContext.Current.Web;

    //check if the current web is a publishing weg
    if (PublishingWeb.IsPublishingWeb(web)) {

        //get the pages list id
        Guid listId = PublishingWeb.GetPagesListId(web);

        //retrieve the pages list
        SPList pagesList = web.Lists[listId];

        //itterate trough the pages
        foreach (SPListItem item in pagesList.Items) {
            //retrieve the terms used for the navigation (this can be multiple terms)
            IList<NavigationTerm> terms = TaxonomyNavigation.GetFriendlyUrlsForListItem(item, false);

            string url = string.Empty;

            //check if the pages has terms associated with it
            if (terms.Count > 0) {
                //use the GetResolvedDisplayUrl to retrieve the page friendly urls
                url = terms[0].GetResolvedDisplayUrl(string.Empty);
            } else {
                //if the page does not have any terms get the normal url
                url = item.File.Url;
            }