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.

30 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
  10. Their transparency, best app design companies and technical know-how make them a valuable partner.

    ReplyDelete
  11. They are miraculously speedy within the response of the requests!
    user experience agency

    ReplyDelete
  12. They delivered top-notch, thorough results that demonstrated expertise and careful attention of detail.
    branding agency

    ReplyDelete
  13. They clearly communicate project changes and updates, and the entire team was professional.
    branding studio

    ReplyDelete
  14. They were communicative and responsive, and they were always available to answer questions.
    best user experience design

    ReplyDelete
  15. The quality of your blogs and conjointly the articles and price appreciating.
    design consultancies San Francisco

    ReplyDelete
  16. I really love your write-ups guys continue the good work.
    brand design firm

    ReplyDelete
  17. Well, it’s a nice one, I have been looking for. Thanks for sharing such informative stuff.
    app designing companies

    ReplyDelete
  18. Hey enormous stuff or pleasant information you are offering here.
    logo design firm

    ReplyDelete
  19. This written piece gives fastidious understanding yet.It’s amazing in support of me to truly have a web site that is valuable meant for my knowledge
    top web agencies

    ReplyDelete
  20. Big thanks to you for sharing such great information.
    best web design company

    ReplyDelete
  21. I see the greatest contents on your blog and I extremely love reading them.
    Bay Area web designer

    ReplyDelete
  22. I have checked this link this is really important for the people to get benefit from.
    UI UX design companies

    ReplyDelete
  23. I wanna thanks to a great extent for providing such informative and qualitative material therefore often.
    UI design firm

    ReplyDelete
  24. 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.
    product designing companies

    ReplyDelete
  25. Thank you I am glad about the encouragement! I love your site, you post outstanding.
    mobile app companies

    ReplyDelete
  26. I'd be trampled if all sites gave articles like these awesome articles.
    best UX agencies

    ReplyDelete
  27. Thanks to share these details it’s truly nice.
    top UI designers

    ReplyDelete
  28. I don’t waste my free time that’s why I read the informative things when I got this blog I really enjoyed reading this.
    Brand Booster US

    ReplyDelete
  29. While the allure of YouTube Cash Cow success is tempting, there are several common pitfalls that aspiring creators should be aware of. From neglecting quality content to focusing solely on monetization, these missteps can hinder long-term growth. By understanding and avoiding these pitfalls, you can set your YouTube Cash Cow journey on the path to success.

    ReplyDelete