XML Sitemap from Episerver Scheduled Job

A quick post on how to write a valid sitemap.xml file to disk. I used this approach in a scheduled job for EPiServer.

There are some dynamic options available to serve a XML sitemap on-the-fly but I preferred having a static file.

I only needed prio 1.0 and "daily" frequency. Enums containing the possible options would be a suitable addition. Here's a snippet with the important parts:

public string Run()
    var items = new List<SiteMapUrl>();
    // Iterate and add the items you want
    foreach ...
        items.Add(new SiteMapUrl
            Loc = "http://krompaco.nu/some-url/",
            Priority = "1.0", // 0.0-1.0 internal prio
            ChangeFreq = "daily", // always, hourly, weekly, monthly, yearly, never
            DateUpdatedUtc = DateTime.Now.ToUniversalTime()

    // Convert items to XML elements
    XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
    var xmlItems = from i in items
                    select new XElement(
                        ns + "url",
                        new XElement(ns + "loc", i.Loc),
                        new XElement(
                        ns + "lastmod", string.Format("{0:yyyy-MM-dd}", i.DateUpdatedUtc)),
                        new XElement(ns + "changefreq", i.ChangeFreq),
                        new XElement(ns + "priority", i.Priority));

    var sitemap = new XDocument(
        new XDeclaration("1.0", "utf-8", "yes"),
        new XElement(
            ns + "urlset",


    return string.Format("Replaced sitemap.xml, now containing {0} locations.", items.Count);

private class SiteMapUrl
    public string Loc { get; set; }

    public DateTime DateUpdatedUtc { get; set; }

    public string ChangeFreq { get; set; }

    public string Priority { get; set; }

Published Friday 31 January 2014 16:19 and tagged with these categories: Episerver, ASP.NET


Add Your Comment