1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / How to create a sitemap user control

How to create a sitemap user control

2 posts, 0 answered
  1. Smita Jain
    Smita Jain avatar
    1 posts
    Registered:
    01 Oct 2009
    12 Jan 2010
    Link to this post

    I want to know how to create a sitemap control in sitefinity using asp.net and c#.

    The control should generate the sitemap in XML format.

    If you know please help.

    Cheers

    Smita

  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    12 Jan 2010
    Link to this post
    Hi Smita Jain,

    You can use standard ASP.NET methods as XmlTextWriter and XmlNode. You can also use LINQ. Below you can find two samples

    sample

    var manager = new CmsManager();
               IList allpages = manager.GetPages();
               foreach (ICmsPage p in allpages)
               {
                   XmlTextWriter writer = new XmlTextWriter(Server.MapPath("test.sitemap"), Encoding.UTF8);
                   writer.WriteStartDocument();
                   writer.WriteStartElement("siteMapNode");
                   writer.WriteElementString("siteMapNode", p.MenuName);
      
                   ....
                        ....
                             // implement logic for adding attributes
                   writer.WriteEndElement();
      
                   ....
                         ....
                       //finally you have to close the writer.
                   writer.Close();
               }


    sample with Linq

     
        // use CmsManager() object  and its GetPages() method to get all pages
        XDocument xDoc = new XDocument(
                    new XDeclaration("1.0", "UTF-8", null),
                    new XElement(schema + "siteMap",
                        new XElement(schema + "siteMapNode", new XAttribute("title", "My Favorites"),
                            new XElement(schema + "siteMapNode", new XAttribute("title", "Favorite Sites"),
                            new XElement(schema + "siteMapNode", new XAttribute("title", "sitefinitywatch"), new XAttribute("url", "http://www.sitefinitywatch.com")),
                            new XElement(schema + "siteMapNode", new XAttribute("title", "telerik"), new XAttribute("url", "http://www.telerik.com")),
                            new XElement(schema + "siteMapNode", new XAttribute("title", "telerik demo"), new XAttribute("url", "http://demos.telerik.com")),
                            new XElement(schema + "siteMapNode", new XAttribute("title", "demo sitefinity"), new XAttribute("url", "http://demo.sitefinity.com"))
                            ),
     
                        new XElement(schema + "siteMapNode", new XAttribute("title", "Teleric corp"),
                            new XElement(schema + "siteMapNode", new XAttribute("title", "telerik"), new XAttribute("url", "http://www.telerik.com"),
                            new XElement(schema + "siteMapNode", new XAttribute("title", "google"), new XAttribute("url", "http://www.google.com")),
                            new XElement(schema + "siteMapNode", new XAttribute("title", "telerik"), new XAttribute("url", "http://www.telerik.com")),
                            new XElement(schema + "siteMapNode", new XAttribute("title", "sitefinity"), new XAttribute("url", "www.sitefinity.com"))
                            )))
     
                            ));
        xDoc.Save(Server.MapPath("Web.sitemap"));
    }


    Greetings,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
2 posts, 0 answered