none
How to create dynamic Sitemap for SharePoint sitecollections and their subsites RRS feed

  • Question

  • Hi All,

    I have created a webapplication in that i have morethan 10 sitecollections and each site collection has morethan 10sites and subsites. I have a client requirement that they need a sitemap which shows how many site collections, sites and its subsites, it should be dynamic (when ever a new site is created then immediately it has to be reflect in sitemap).

    1) Can this be possible through OOB? If yes please do let me know how to implement this.

    2) Can be possible through custom coding using UserControl.ascx?

    Please let me know how can i implement sitemap feature. Thanks in advance


    Mohammed
    Thursday, April 7, 2011 6:10 AM

Answers

  • Using ascx i have do this recently.

    I have created a tree control.

    Below is the code for the same.

    //Displays the site hierarchy
    public TreeView trvSite;
    
    //Register eventhandler
    this.trvSite.SelectedNodeChanged += new EventHandler(trvSite_SelectedNodeChanged);
    
    //On Page load event 
    try
            {
              SPWebApplication webApp = SPContext.Current.Site.WebApplication;
    
              if (webApp != null)
              {
                foreach (SPSite site in webApp.Sites)
                {
                  try
                  {
                    //If user do not have permission, access denied page will be shown, here we dont want that
                    site.CatchAccessDeniedException = false;
                    TreeNode newNode = new TreeNode(site.RootWeb.Title, site.Url);
                    newNode.SelectAction = TreeNodeSelectAction.SelectExpand;
                    this.trvSite.Nodes.Add(newNode);
                  }
                  catch (Exception ex)
                  {
                    //Consume execption, so we can go to the next site
                  }
                }
              }
            }
            catch (Exception ex)
            {          
              Response.Write(ex.Message);
            }
    
    
    //Event handler
    void trvParentSite_SelectedNodeChanged(object sender, EventArgs e)
        {
          try
          {
            using (SPSite site = new SPSite(this.trvParentSite.SelectedNode.Value))
            {
              try
              {
                site.CatchAccessDeniedException = false;
                using (SPWeb web = site.OpenWeb())
                {
                  if (web.Webs.Count != 0)
                  {
                    foreach (SPWeb childWebs in web.Webs)
                    {
                      try
                      {
                        TreeNode newNode = new TreeNode(childWebs.Title, childWebs.Url);
                        newNode.SelectAction = TreeNodeSelectAction.SelectExpand;
                        trvSite.SelectedNode.ChildNodes.Add(newNode);
                      }
                      catch { }
                    }
                  }
                }
              }
              catch { }
            }
          }
          catch
          {
          }
        }
    


    Thanks,
    A.m.a.L
    [MVP Visual C#]
    Dot Net Goodies
    Don't hate the hacker, hate the code
    • Marked as answer by David HM Friday, April 15, 2011 1:35 AM
    Thursday, April 7, 2011 6:55 AM

All replies

  • Hi,

    This could be possible using SSRS report.

    1. Create a ssrs report and query against dbo.webs and dbo.sites table of respective content databases. This is dynamic and you can publish this report to Sharepoint.

    2. OOB, using site directory template we can force to list the newly created site collections to display in sites directory , this will also display the site map.please check if this works with your scenario or not.

    In this scenario i would suggest to go with step1.


    Best Regards, Ashok Yadala
    Thursday, April 7, 2011 6:54 AM
  • Using ascx i have do this recently.

    I have created a tree control.

    Below is the code for the same.

    //Displays the site hierarchy
    public TreeView trvSite;
    
    //Register eventhandler
    this.trvSite.SelectedNodeChanged += new EventHandler(trvSite_SelectedNodeChanged);
    
    //On Page load event 
    try
            {
              SPWebApplication webApp = SPContext.Current.Site.WebApplication;
    
              if (webApp != null)
              {
                foreach (SPSite site in webApp.Sites)
                {
                  try
                  {
                    //If user do not have permission, access denied page will be shown, here we dont want that
                    site.CatchAccessDeniedException = false;
                    TreeNode newNode = new TreeNode(site.RootWeb.Title, site.Url);
                    newNode.SelectAction = TreeNodeSelectAction.SelectExpand;
                    this.trvSite.Nodes.Add(newNode);
                  }
                  catch (Exception ex)
                  {
                    //Consume execption, so we can go to the next site
                  }
                }
              }
            }
            catch (Exception ex)
            {          
              Response.Write(ex.Message);
            }
    
    
    //Event handler
    void trvParentSite_SelectedNodeChanged(object sender, EventArgs e)
        {
          try
          {
            using (SPSite site = new SPSite(this.trvParentSite.SelectedNode.Value))
            {
              try
              {
                site.CatchAccessDeniedException = false;
                using (SPWeb web = site.OpenWeb())
                {
                  if (web.Webs.Count != 0)
                  {
                    foreach (SPWeb childWebs in web.Webs)
                    {
                      try
                      {
                        TreeNode newNode = new TreeNode(childWebs.Title, childWebs.Url);
                        newNode.SelectAction = TreeNodeSelectAction.SelectExpand;
                        trvSite.SelectedNode.ChildNodes.Add(newNode);
                      }
                      catch { }
                    }
                  }
                }
              }
              catch { }
            }
          }
          catch
          {
          }
        }
    


    Thanks,
    A.m.a.L
    [MVP Visual C#]
    Dot Net Goodies
    Don't hate the hacker, hate the code
    • Marked as answer by David HM Friday, April 15, 2011 1:35 AM
    Thursday, April 7, 2011 6:55 AM
  • OTB site map providers works within single site collection, so in order to achieve your goals you need to develop custom site map provider which will iterate thorugh all site collections and sites recursively and build site map. You can check my posts about related theme:

    The basics of navigation in Sharepoint - describes the basic architecture of navigation and site maps in Sharepoint

    Cross-site and cross-site collection navigation in Sharepoint - part 1

    Cross-site and cross-site collection navigation in Sharepoint - part 2: publishing sites

    last 2 posts contains example how to implement custom site map provider for non-publishing and publishing sites.

    One thing you should address: site map provider is called is very often in real life, so you need to add some caching logic in order to avoid performance impact. It is better to keep it in mind during implementation instead of applying afterward.


    Blog - http://sadomovalex.blogspot.com
    CAML via C# - http://camlex.codeplex.com
    Thursday, April 7, 2011 6:57 AM
  • Hi,

    you can use Table of Contents web part to achieve this OOB

    and here is link (Moderator: to "Add a table of contents to a publishing page layout") for you to get started


    Warm Regards, Bhushan http://www.passionatetechie.blogspot.com


    • Edited by Bhushan Gawale Monday, April 11, 2011 6:42 AM reference link added
    • Edited by Mike Walsh FIN Wednesday, September 21, 2011 7:05 AM "here is a link" could be anything. Say what you are linking to and show the URL in your text
    Friday, April 8, 2011 12:26 PM
  • Hi A.m.a.L

    Using SPWebApplication webApp = SPContext.Current.Site.WebApplication; in page load of webpart gives me an access denied error. Any thoughts. I did try using Runwithelevatedprevileges too.

    Wednesday, September 21, 2011 4:00 AM