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"
<%@ Page Language="C#" ContentType="application/xml"%>
Next,we create references to the necessary SharePoint and XML assemblies:
<%@ Import Namespace="Microsoft.SharePoint.Administration" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Import Namespace="System.Xml" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
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:
<%@ Page Language="C#" ContentType="application/xml"%>
<%@ Import Namespace="Microsoft.SharePoint.Administration" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Import Namespace="System.Xml" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<script runat="server">
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<SPWebService>("");
// Prepare the XML Writer
XmlTextWriter xmlWriter = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
//Start the XML Document
xmlWriter.WriteStartDocument();
//Create a Web Applications Element
xmlWriter.WriteStartElement("webapplications");
//**********************************************
// 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.
//***********************************************
try
{
//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.WriteStartElement("webapplication");
xmlWriter.WriteAttributeString("name", webApp.DisplayName);
try
{
//Create a sites element for the site collections
xmlWriter.WriteStartElement("sites");
//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.WriteStartElement("site");
xmlWriter.WriteAttributeString("url", siteCollection.Url);
//call the recursive method to get all the sites(webs)
GetWebs(siteCollection.AllWebs, xmlWriter);
//close the site element
xmlWriter.WriteEndElement();
}
//close the site collection element
xmlWriter.WriteEndElement();
}
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
xmlWriter.WriteEndElement();
}
}
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
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Close();
}
//*************************************************
// 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)
xmlWriter.WriteStartElement("webs");
try
{
//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.WriteStartElement("web");
xmlWriter.WriteAttributeString("url",web.Url);
xmlWriter.WriteAttributeString("title", web.Title);
xmlWriter.WriteAttributeString("WebTemplateID", web.WebTemplateId.ToString());
xmlWriter.WriteAttributeString("WebTemplateName", web.WebTemplate);
//close the site(web) element
xmlWriter.WriteEndElement();
}
}
}
catch (Exception webError)
{
//if an error occurs write the error message to
//an error element
xmlWriter.WriteElementString("error", webError.Message);
}
//close the webs element
xmlWriter.WriteEndElement();
}
</script>
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:
- 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.
- Open the folder and create a new empty file naming it FEATURE.xml
- Open the file and paste the following XML, replacing NEWGUID with a newly generated GUID.
<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/"
Id="NEWGUID"
Title="Enumerate Sites in XML"
Hidden="FALSE"
Scope="Farm"
ActivateOnDefault="TRUE"
Version="12.0.0.0">
<ElementManifests>
<ElementManifest Location="Elements.xml" />
</ElementManifests>
</Feature>
- Save and close the file.
- 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):
<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomActionGroup Id="GROUPGUID"
Location="Microsoft.SharePoint.Administration.ApplicationManagement"
Title="Application Management Utilities" Sequence="1010" />
<CustomAction
Id="ACTIONGUID"
GroupId="GROUPGUID"
Location="Microsoft.SharePoint.Administration.ApplicationManagement"
Sequence="10" Title="Enumerate Sites in XML" Description="">
<UrlAction Url="_admin/enumeratesites.aspx" />
</CustomAction>
</Elements>
- Save and close the file
- Package the EnumerateSites.aspx page and feature using your method of choice. Don't have one? Checkout WSPBuilder by Carsten Keutmann
- 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