November Happy Hour will be moved to Thursday December 5th.

Johan Björnfot
Apr 14, 2011
  5064
(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
Optimizely SaaS CMS + Coveo Search Page

Short on time but need a listing feature with filters, pagination, and sorting? Create a fully functional Coveo-powered search page driven by data...

Damian Smutek | Nov 21, 2024 | Syndicated blog

Optimizely SaaS CMS DAM Picker (Interim)

Simplify your Optimizely SaaS CMS workflow with the Interim DAM Picker Chrome extension. Seamlessly integrate your DAM system, streamlining asset...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Optimizely CMS Roadmap

Explore Optimizely CMS's latest roadmap, packed with developer-focused updates. From SaaS speed to Visual Builder enhancements, developer tooling...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Set Default Culture in Optimizely CMS 12

Take control over culture-specific operations like date and time formatting.

Tomas Hensrud Gulla | Nov 15, 2024 | Syndicated blog

I'm running Optimizely CMS on .NET 9!

It works 🎉

Tomas Hensrud Gulla | Nov 12, 2024 | Syndicated blog

Recraft's image generation with AI-Assistant for Optimizely

Recraft V3 model is outperforming all other models in the image generation space and we are happy to share: Recraft's new model is now available fo...

Luc Gosso (MVP) | Nov 8, 2024 | Syndicated blog