Johan Björnfot
Apr 14, 2011
  5055
(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
CMS + CMP + Graph integration

We have just released a new package https://nuget.optimizely.com/package/?id=EPiServer.Cms.WelcomeIntegration.Graph which changes the way CMS fetch...

Bartosz Sekula | Nov 5, 2024

Block type selection doesn't work

Imagine you're trying to create a new block in a specific content area. You click the "Create" link, expecting to see a CMS modal with a list of...

Damian Smutek | Nov 4, 2024 | Syndicated blog

.NET 8 FAQ

I have previously written about .NET compatibility in general and .NET 8 in particular, see blog posts here , here and here . With the end of suppo...

Magnus Rahl | Nov 4, 2024

Dynamic packages in Commerce Connect

In Optimizely Commerce Connect, you can group different items using packages and bundles. Package: A package has one or more versions of a product...

K Khan | Nov 1, 2024

Efficient Catalog Metadata Management and Product Updates Using DTOs in Optimizely Commerce

This post explores ways to manage and update catalog metadata in Optimizely Commerce by utilizing Data Transfer Objects (DTOs). DTOs provide a...

Sujit Senapati | Oct 31, 2024

Effortlessly Resize Images with Cloudflare's On-the-Fly Solution

Resizing images in C# has traditionally been a complex and time-consuming task, often requiring intricate code and handling various image processin...

Manoj Kumawat | Oct 31, 2024 | Syndicated blog