Updated 8/25/2008: Updates to formatting in code blocks and removed recursive call that was causing duplication.

In my previous post “Iterating through SharePoint Web Applications, Site Collections, and Sites (Webs)” I provided a simple script to iterate through all web applications, sites and site collections. In this post, I will use much of that code to create an application page that returns a list of all the sites in the farm in XML. I will also create a feature to display a link to the application page from the Application Management section in Central Administration.

Click here to download the Solution Package (WSP)
Click here to download the source files

First I create a new file called EnumerateSites.aspx in c:\program files\common files\microsoft shared\web server extensions\12\template\admin. I will be accessing the page from the Central Administration site; I prefer to put such pages in the “admin” folder which is not accessible from regular SharePoint sites, as opposed to the “layouts” folder.

Since this file will be returning a list of all the sites in XML, we need to make sure the browser renders it properly. To do so we change the content type of the page to “application/xml”

Next,we create references to the necessary SharePoint and XML assemblies:

Since we are only displaying XML we wont need to reference a master page; so the rest is pretty much the same code from the previous post executing in the OnLoad event of the page. I use the XMLTextWriter class to build the XML document, and incorporate a number of “try catch” statements throughout the code to ensure that any errors that may be encountered are properly displayed in the XML. I’ve pasted all of the code for the application page (including the 2 previous excerpts) below:

    protected override void OnLoad(EventArgs e){
        // Get references to the farm and farm WebService object
        // the SPWebService object contains the SPWebApplications
        SPFarm thisFarm = SPFarm.Local;
        SPWebService service = thisFarm.Services.GetValue(“”);

        // Prepare the XML Writer
        XmlTextWriter xmlWriter = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);

        //Start the XML Document

        //Create a Web Applications Element

        // NOTE: From here on everything is executed in
        //       “try catch” blocks. This is done to
        //       facilitate troubelshooting in case any
        //       errors surface. The error is caught and
        //       rendered in an xml “error” element.
        //       since the pages MIME type has been
        //       changed to XML, allowing the error
        //       to surface would render the xml document
        //       unreadable in IE.
            //Iterate through each web application
            foreach (SPWebApplication webApp in service.WebApplications)
                //Create an XML Element for each web application
                //and include the name in the “name” attribute
                xmlWriter.WriteAttributeString(“name”, webApp.DisplayName);
                    //Create a sites element for the site collections

                    //Iterate through each site collection
                    foreach (SPSite siteCollection in webApp.Sites)

                        //Create an XML Element for each site collection
                        //and include the url in the “url” attribute
                        xmlWriter.WriteAttributeString(“url”, siteCollection.Url);

                        //call the recursive method to get all the sites(webs)
                        GetWebs(siteCollection.AllWebs, xmlWriter);

                        //close the site element

                    //close the site collection element

                catch (Exception siteError)
                    //if an error occurs write the error message to
                    //an error element
                    xmlWriter.WriteElementString(“error”, siteError.Message);
                //close the web application element
        catch (Exception webAppError)
            //if an error occurs write the error message to
            //an error element
            xmlWriter.WriteElementString(“error”, webAppError.Message);

        // close the web applications element and document
    // This method is used recursively to display all
    // webs in a site collection. The Web Collection
    // from the site collection is passed in along with
    // the XML writer to continue writing the XML Document
    // where the calling method left off
    public void GetWebs(SPWebCollection allWebs, XmlTextWriter xmlWriter)
        //create a webs element to contain all sites(webs)
            //iterate through each site(web)
            foreach (SPWeb web in allWebs)
                if (web.Permissions.DoesUserHavePermissions(SPRights.FullMask));
                    //Create an XML Element for each site(web)
                    //and include attributes for the url, title,
                    //and template information
                    xmlWriter.WriteAttributeString(“title”, web.Title);
                    xmlWriter.WriteAttributeString(“WebTemplateID”, web.WebTemplateId.ToString());
                    xmlWriter.WriteAttributeString(“WebTemplateName”, web.WebTemplate);

                    //close the site(web) element
        catch (Exception webError)
                //if an error occurs write the error message to
                //an error element   
                xmlWriter.WriteElementString(“error”, webError.Message);
        //close the webs element   

With the page saved in the admin folder, you could just access it via the URL of your central administration site:
(i.e. http://centraladminsite/_admin/enumeratesites.aspx)

I personally prefer to build a Custom Action which adds a link to the page from Application Management in Central Administration. The steps to do this are relatively simple:

  1. Create a new folder for the feature and call it “EnumerateSites”. We will use the feature to add a link to the page from Central Administration.
  2. Open the folder and create a new empty file naming it FEATURE.xml
  3. Open the file and paste the following XML, replacing NEWGUID with a newly generated GUID.

    <Feature xmlns="
    Title=”Enumerate Sites in XML”

  4. Save and close the file.
  5. Create another file, this time naming it “Elements.xml” and paste the following XML, replacing GROUPGUID and ACTIONGUID with newly generated GUIDs (use the same GUID in both occurrences of GROUPGUID):

    <Elements xmlns="http://schemas.microsoft.com/sharepoint/“>
    <CustomActionGroup Id="GROUPGUID
    Title=”Application Management Utilities” Sequence=”1010” />
    Sequence=”10” Title=”Enumerate Sites in XML” Description=”“>

    <UrlAction Url="_admin/enumeratesites.aspx” />

  6. Save and close the file
  7. Package the EnumerateSites.aspx page and feature using your method of choice. Don’t have one? Checkout WSPBuilder by Carsten Keutmann
  8. Deploy your solution and you are done!

You can access the page from the Application Management section of Central Administration, you’ll see a new section titled Application Management Utilities with a link to Enumerate Sites in XML.


Click on the link to get an XML page enumerating all of the sites in your farm, it should look similar to the following


Click here to download the Solution Package (WSP)
Click here to download the source files