Surendra Sharma

Surendra Sharma

Search This Blog

Thursday, April 16, 2015

How to create XML Sitemap for Sitecore website programmatically

There are time when you are going to deploy your website on production and client want Sitemap file of website. Sitemap file is used to improve SEO results.

Sometimes client have some weird requirement that create XML sitemap dynamically for Sitecore website.

For this type of requirement, always keep one field "Show Sitemap" in all the page content.

Here is a code to generate sitemap dynamically. Remember you need a permission to update Sitemap.xml file on server.

public bool GenerateXml(List<Tuple<string, string, string>> lstTuples)  //generateXml
{
    bool result = false;
    string xmlFile = HttpContext.Current.Server.MapPath("/Sitemap.xml");

    XmlTextWriter writer = new XmlTextWriter(xmlFile, System.Text.Encoding.UTF8);
    writer.Formatting = Formatting.Indented;
    writer.WriteStartDocument();
    {
        writer.WriteStartElement("urlset");
        writer.WriteAttributeString("xmlns:xhtml", "http://www.w3.org/1999/xhtml");
        writer.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");

        string url = "http://" + System.Web.HttpContext.Current.Request.ServerVariables["HTTP_HOST"];

        foreach (var value in lstTuples)
        {
            writer.WriteStartElement("url");
            writer.WriteElementString("loc", System.Web.HttpUtility.HtmlEncode(url + value.Item2));
            writer.WriteElementString("lastmod", value.Item3);
            writer.WriteElementString("changefreq", "weekly");
            writer.WriteElementString("priority", "0.5");
            writer.WriteEndElement();
        }

        writer.WriteEndElement();
        writer.WriteEndDocument();
        writer.Flush();

        result = true;
    }

    return result;

}

Pass the Tuple collection which contains URL and last modified date of content items.
This code should create file with below data

<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>http://test/</loc>
    <lastmod>2015-04-06</lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.5</priority>
  </url>
  <url>
    <loc>http://test/Main Menu/Page1</loc>
    <lastmod>2015-03-23</lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.5</priority>
  </url>


Please leave your comments or share this code if it’s useful for you.

10 comments:

  1. It does not need to be a static Sitemap.xml file. You can call it anything. Important thing is that you tell on robots.txt where it is located. Just make an item and add layout to it and then just dynamically load it. For example /sitemapxml.aspx. All you need to make sure is this should load in less than 30 secs or Googlebot will give up.

    ReplyDelete
  2. Whether somebody pursuit of his vital thing, hence he or she desires to be accessible that at length, hence that thing is maintained over here.
    Ministry of Freedom bonus

    ReplyDelete
  3. The team was responsive, professional, and easy to work with best brand agencies. Their strong deliverables and project management best branding agency have also secured continued engagement.

    ReplyDelete
  4. They successfully delivered the marketing and UX design companies websites to the delight of the sales team. Their strong expertise informed painful decision industrial ui services making and strategy.

    ReplyDelete
  5. It’s a brilliant platform concerning one of our primary issues! weed scale

    ReplyDelete
  6. Communication issues haven't disrupted the premium niche edit project timeline or impacted the quality of end-deliverables.

    ReplyDelete
  7. The stated web site is like a first rate help for all the humans attempting to connect with a person like them, for similar records take a look at their internet site! They may be incredible!
    website design company

    ReplyDelete
  8. Their team communicated with designers and exceeded expectations. They also helped with market research.
    top branding companies

    ReplyDelete
  9. Their communication skills and reliable contact were features of the collaboration
    digital branding agency

    ReplyDelete