Sitemap and Robots Generator Filter Programmatically by Type

Sitemap and Robots Generator Filter Programmatically by Type

This blog post is a continuation to the blog post about the Sitemap and Robots Generator for Episerver CMS, where you can find how to install and configure the plugin and the blog post about the Sitemap and Robots Generator for Commerce Episerver CMS, which explain how to add products to the sitemap file. In here, we are going to explain how we can configure the plugin to filter programmatically the sitemap items by page or block type. In addition, we will use the latest version available of the plugin which at the time of writing is (Version is only for CMS 12). You can find the plugin developed by Verndale in this link. If you cannot find the latest version in the episerver feed please use the nuget feed instead.

Install-Package Verndale.Sitemap.Robots.Generator -Version

As soon as you have your plugin installed and configured properly. We will start creating the classes needed to filter the sitemap items by type. So without further due, lets begin.

First, we will create a custom sitemap filter class which inherits from the ISitemapFilter interface, this class is responsible to filter the items by type, but keep in my mind that you can only have one sitemap filter running at the same time. In the code below we are filtering out faq pages and any other type is included. If you want to add more filters just add more if clauses.

public class CustomSitemapFilter : ISitemapFilter
    // Include item based on custom implementation
    public bool IncludeItem(IContent item)
        if(item is FaqPage){
            return false;
        return true;

The Faq page for completion purposes has the following code.

using EPiServer.Core;
using EPiServer.DataAnnotations;
using EPiServer.Web;
using System.ComponentModel.DataAnnotations;

namespace Foundation.Features.Faq.Pages.FaqPage
    [ContentType(DisplayName = "FAQ Page",
        GUID = "D01A2AEB-C50F-4069-8C31-6E093FCF187F",
        Description = "Display a page for Frequented Asked Questions in the site",
        AvailableInEditMode = true,
        GroupName = "Content")]
    public class FaqPage : FoundationPageData
        [Display(Name = "FAQ Heading", Order = 100)]
        public virtual string FaqHeading { get; set; }

        [Display(Name = "FAQ Copy", Order = 200)]
        public virtual string FaqCopy { get; set; }

        [Display(Name = "FAQ Questions", Order = 300)]
        public virtual IList<string> Questions { get; set; }
        [Display(Name = "FAQ Answers", Order = 300)]
        public virtual IList<string> Answers { get; set; }

Finally, you must add the just implemented filter to replace the implementation provided by the plugin. To do this you must add the following code wherever you have your structure map initializations.

if (context.Services.Contains(typeof(ISitemapFilter)))
    context.Services.AddTransient<ISitemapFilter, CustomSitemapFilter>();

And that is it. Now you can have a class which can programmatically filter some types to appear in your sitemap. If you have any questions or suggestions please let me know in the comments. I hope this blog post can help someone and as always keep learning !!!

Written by:

Jorge Cardenas

Developer with several years of experience who is passionate about technology and how to solve problems through it.

View All Posts

Leave a Reply