Friday, April 02, 2010

Sharepoint MOSS 2007 enum all site collections

unfortunate I don't remember from where I have takeover the following code - if you find it let me know - i would like to update a link to your site.

copy and paste following code into a enum.aspx and than save the file on your sharepoint 2007 / MOSS 2007 under the following path:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI\enum.aspx

once you're done with that you can invoke it under the following url: http://your.sharepoint.com/_vti_bin/Enum.aspx

NOTE: to use this you have to call it with a user which have enough privileges for all site collections - best would be to call it with the app-pool user.

<%@ 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>

2 comments:

Darren said...

The code came from here: http://blog.rafelo.com/2008/07/sharepoint-site-enumeration-page-get.html

Anonymous said...

The code came from here: http://blog.rafelo.com/2008/07/sharepoint-site-enumeration-page-get.html

Shared Cache - .Net Caching made easy

All information about Shared Cache is available here: http://www.sharedcache.com/. Its free and easy to use, we provide all sources at codeplex.

Facebook Badge