Johan Björnfot
Apr 14, 2011
  5085
(2 votes)

Ensure unique url segments for read-only page providers

During save of a page EPiServer CMS will ensure that the url segment for the page is unique (with unique means that there is no other page on the same language with same parent that has the same url segment). This means that pages that are created through the EPiServer API will get unique url segments. However for read-only page providers it is up to the page provider itself to ensure that the url segments are unique (we might add helper functions for this in the product in the future). Currently if the provider uses method InitializePageData to initialize PageData instances the url segment will be set to the same as the page name. So if there are many pages at the same level with same page name in same language they will get the same url segment.

Below is a helper class that can be used in a read only page provider to ensure that the url segments will be unique. Note that this implementation does not store the generated url segment in a persistent storage (for example database). This means that there is no guarantee that a specific page gets the same url segment after an application restart. Instead the url segment is dependent on the request order of the pages that has the same page name.

First in your read only page provider add a field like: 

UrlSegmentHandler _urlSegmentHanlder = new UrlSegmentHandler();

Then in method GetLocalPage after the call to InitializePageData you add a call like:

_urlSegmentHanlder.EnsureUniqueUrlSegment(page);

The class UrlSegmentHanlder looks like:

public class UrlSegmentHandler
{
    private Dictionary<string, LanguageUrlSegments> _languageSegmentHandlers = 
        new Dictionary<string, LanguageUrlSegments>();

    public void EnsureUniqueUrlSegment(PageData page)
    {
        LanguageUrlSegments languageSegments;
        if (!_languageSegmentHandlers.TryGetValue(page.LanguageBranch, 
            out languageSegments))
        {
            languageSegments = new LanguageUrlSegments();
            _languageSegmentHandlers.Add(page.LanguageBranch, languageSegments);
        }

        languageSegments.EnsureUniqueUrlSegment(page);
    }
}

public class LanguageUrlSegments
{
    private Dictionary<PageReference, string> _pageUrlSegments =
        new Dictionary<PageReference, string>();

    //Dictionary <Parent, Dictionary<urlSegment, PageLink>>
    private Dictionary<PageReference, Dictionary<string, PageReference>> _urlSegments = 
        new Dictionary<PageReference, Dictionary<string, PageReference>>();

    public void EnsureUniqueUrlSegment(PageData page)
    {
        //NOTE: Here we expect that PageLink, ParentLink and UrlSegment is set
        //Which they are if InitializePageData has been called.

        //If we already generated a urlsegment for page, return it
        string urlSegment; 
        if (_pageUrlSegments.TryGetValue(page.PageLink, out urlSegment))
        {
            page.URLSegment = urlSegment;
            return;
        }

        urlSegment = page.URLSegment;
        //First get all urlsegments under parent
        Dictionary<string, PageReference> existingUrlSegments;
        if (!_urlSegments.TryGetValue(page.ParentLink, out existingUrlSegments))
        {
            existingUrlSegments = new Dictionary<string, PageReference>();
            _urlSegments.Add(page.ParentLink, existingUrlSegments);
        }

        PageReference existingPage;
        if (!existingUrlSegments.TryGetValue(urlSegment, out existingPage))
        {
            existingPage = page.PageLink;
            _pageUrlSegments[page.PageLink] = urlSegment;
            existingUrlSegments.Add(page.URLSegment, page.PageLink);
        }

        //In case there is another page with same segment "postfix"
        //urlsegment with counter until it is unique.
        int counter = 0;
        while (!existingPage.CompareToIgnoreWorkID(page.PageLink))
        {
            urlSegment = page.URLSegment + (++counter).ToString();
            if (!existingUrlSegments.TryGetValue(urlSegment, out existingPage))
            {
                existingPage = page.PageLink;
                _pageUrlSegments[page.PageLink] = urlSegment;
                existingUrlSegments.Add(urlSegment, page.PageLink);
            }
        }

        //Set the unique segment on page
        page.URLSegment = urlSegment;
    }
}
Apr 14, 2011

Comments

Apr 14, 2011 10:59 AM

Awesome! :)

Please login to comment.
Latest blogs
How to add an Admin Mode add-on in Optimizely CMS12

How to add a new add-on with navigation and unified stylesheet

Bartosz Sekula | Jan 2, 2025 | Syndicated blog

Managing Your Graph Conventions

Recently, Optimizely released a Conventions API for manging how various fields on your CMS content are indexed by the Graph. This is an extremely...

Ethan Schofer | Dec 31, 2024

SaaS CMS and Visual Builder - Opticon 2024 Workshop Experience

Optimizely is getting SaaSy with us…. This year Optimizely’s conference Opticon 2024 took place in San Antonio, Texas. There were a lot of great...

Raj Gada | Dec 30, 2024

Copy Optimizely SaaS CMS Settings to ENV Format Via Bookmarklet

Do you work with multiple Optimizely SaaS CMS instances? Use a bookmarklet to automatically copy them to your clipboard, ready to paste into your e...

Daniel Isaacs | Dec 22, 2024 | Syndicated blog

Increase timeout for long running SQL queries using SQL addon

Learn how to increase the timeout for long running SQL queries using the SQL addon.

Tomas Hensrud Gulla | Dec 20, 2024 | Syndicated blog

Overriding the help text for the Name property in Optimizely CMS

I recently received a question about how to override the Help text for the built-in Name property in Optimizely CMS, so I decided to document my...

Tomas Hensrud Gulla | Dec 20, 2024 | Syndicated blog