Modify content items default URLs

Read time: 8 min

In Sitefinity out-of-the-box content items as News, Events, Blogs, etc. have their own URL format. For example Blog posts have the following default URL format:

[domain]/[page]/[blog-url-name]/[publication-date]/[post-url-name]

The URL format is controlled by the default provider class for the content items. For Blog posts this is OpenAccessBlogsProvider class. This creates consistency in the usage of content items.

However sometimes there are cases when you will need your own custom format of the content item URL. One serious constraint is that for events the calendar name is not translated and even if you display events in another language the URL will still show the calendar name in English.

Solution

Let's aim at presenting Blog post URLs as follows:

[domain]/[page]/[post-url-name]

The post will not contain the Publication date and the parent Blog in the URL format.

To achieve this you need to create your own class that inherits the default provider. For example for Blog Posts you can create a CustomBlogsProvider and add the following properties inside the class:

    /// <summary>
    /// The URL format string for blog items
    /// </summary>
    public string BlogUrlFormat
    {
        get;
        set;
    }

    /// <summary>
    /// The URL format string for blog post items
    /// </summary>
    public string BlogPostUrlFormat
    {
        get;
        set;
    }

The properties that you added are needed as they will store the URL format for blogs and blog posts.

TIP: You can add automatic XML code comments to your properties via GhostDoc as shown above. It represents a Visual Studio extension. For more information, see GhostDoc official page here.

As a next, step you need to override the *GetUrlFormat *method, part of the OpenAccessBlogsProvider parent class:

public override string   GetUrlFormat(Telerik.Sitefinity.GenericContent.Model.ILocatable item)
    {
        if (item.GetType() == typeof(Blog))
        { 
             return this.BlogUrlFormat; 
        }
        else if (item.GetType() == typeof(BlogPost))
        {
            return this.BlogPostUrlFormat;
        }
        else
        {
            return base.GetUrlFormat(item);
        }
    }

The method is used to return the URL format for a specific item. In this case either a Blog or a Blog post.

The logic to modify the default URL format for Blog post items is done in InitializeControls method as shown below:

///<inheritdoc />    
protected override void Initialize(string providerName, NameValueCollection config,    Type managerType)
    {
        base.Initialize(providerName, config, managerType);
        this.BlogUrlFormat = config["blogUrlFormat"];
        if (string.IsNullOrEmpty(this.BlogUrlFormat))
        {
            this.BlogUrlFormat = "/[UrlName]";
        }
        this.BlogPostUrlFormat = config["blogPostUrlFormat"];
        if (string.IsNullOrEmpty(this.BlogPostUrlFormat))
        {
            this.BlogPostUrlFormat = "/[Parent.UrlName]/[PublicationDate, {0:yyyy'/'MM'/'dd}]/[UrlName]";
        }
    }

TIP: Use inheritdoc when overriding methods. That way the XML documentation of the base method will be used.

As a final step, the CustomBlogsProvider needs to be registered in Sitefinity so that it replaces the default one. Navigate to Administration >> Settings >> Advanced >>Blogs >> Providers >> OpenAccessDataProvider. In the ProviderType field, the custom provider’s type must be specified as shown in the picture below:

The format for blog posts URL format must be changed under the parameters node of the OpenAccessDataProvider – e.g. blogPostUrlFormat parameter with value of /[UrlName].

As a result when a Blog Post is opened in detail view mode, the URL will only display the post URL name.

Veronica Milcheva

About Veronica Milcheva

I am a passionate Sitefinity blogger, developer and consultant. In my spare time I enjoy running and listening to music. My personal quote: There's no tough problem, just not enough coffee :)

View Comments

comments powered by Disqus