Listing and Formatting MOSS Site Collections#

One of the useful things stsadm.exe can give you is a list of all current site collections which is useful for analysisng site data, or displaying site collection owners for self service driven services, or for simply gathering trend data to help analyse growth over time. 

It doesn't however lend itself to easy presentation within Sharepoint 2007.  The XML output is a bit raw so to speak.  Its difficult to add an XSLT to it, even with the XML web part and while its good in a spreadsheet its not very good for use within MOSS itself. 

The output of running stsadm is something like this

<Sites Count="15">

<Site Url="http://sharepoint.com/sites/john1" Owner="ad\timney" SecondaryOwner="ad\admin" ContentDatabase="selfservice01db-01" StorageUsedMB="0.5" StorageWarningMB="40" StorageMaxMB="50" />

<Site Url="http://sharepoint.com/sites/mac1" Owner="ad\jobs" SecondaryOwner="ad\admin" ContentDatabase="selfservice01db-01" StorageUsedMB="52" StorageWarningMB="900" StorageMaxMB="1000" />

<Site Url="http://sharepoint.com/sites/john2" Owner="ad\timney" SecondaryOwner="ad\admin" ContentDatabase="selfservice01db-01" StorageUsedMB="0.5" StorageWarningMB="40" StorageMaxMB="50" />

<Site Url="http://sharepoint.com/sites/mac2" Owner="ad\jobs" SecondaryOwner="ad\admin" ContentDatabase="selfservice01db-01" StorageUsedMB="52" StorageWarningMB="900" StorageMaxMB="1000" />

<Site Url="http://sharepoint.com/sites/john3" Owner="ad\timney" SecondaryOwner="ad\admin" ContentDatabase="selfservice01db-01" StorageUsedMB="0.5" StorageWarningMB="40" StorageMaxMB="50" />

<Site Url="http://sharepoint.com/sites/mac3" Owner="ad\jobs" SecondaryOwner="ad\admin" ContentDatabase="selfservice01db-01" StorageUsedMB="52" StorageWarningMB="900" StorageMaxMB="1000" />

<Site Url="http://sharepoint.com/sites/john4" Owner="ad\timney" SecondaryOwner="ad\admin" ContentDatabase="selfservice01db-01" StorageUsedMB="0.5" StorageWarningMB="40" StorageMaxMB="50" />

<Site Url="http://sharepoint.com/sites/mac4" Owner="ad\jobs" SecondaryOwner="ad\admin" ContentDatabase="selfservice01db-01" StorageUsedMB="52" StorageWarningMB="900" StorageMaxMB="1000" />

<Site Url="http://sharepoint.com/sites/john5" Owner="ad\timney" SecondaryOwner="ad\admin" ContentDatabase="selfservice01db-01" StorageUsedMB="0.5" StorageWarningMB="40" StorageMaxMB="50" />

<Site Url="http://sharepoint.com/sites/mac5" Owner="ad\jobs" SecondaryOwner="ad\admin" ContentDatabase="selfservice01db-01" StorageUsedMB="52" StorageWarningMB="900" StorageMaxMB="1000" />

<Site Url="http://sharepoint.com/sites/john6" Owner="ad\timney" SecondaryOwner="ad\admin" ContentDatabase="selfservice01db-01" StorageUsedMB="0.5" StorageWarningMB="40" StorageMaxMB="50" />

<Site Url="http://sharepoint.com/sites/mac6" Owner="ad\jobs" SecondaryOwner="ad\admin" ContentDatabase="selfservice01db-01" StorageUsedMB="52" StorageWarningMB="900" StorageMaxMB="1000" />

<Site Url="http://sharepoint.com/sites/john7" Owner="ad\timney" SecondaryOwner="ad\admin" ContentDatabase="selfservice01db-01" StorageUsedMB="0.5" StorageWarningMB="40" StorageMaxMB="50" />

<Site Url="http://sharepoint.com/sites/mac7" Owner="ad\jobs" SecondaryOwner="ad\admin" ContentDatabase="selfservice01db-01" StorageUsedMB="52" StorageWarningMB="900" StorageMaxMB="1000" />

<Site Url="http://sharepoint.com/sites/john8" Owner="ad\timney" SecondaryOwner="ad\admin" ContentDatabase="selfservice01db-01" StorageUsedMB="0.5" StorageWarningMB="40" StorageMaxMB="50" />

</Sites>

One advantage though is that it is bindable within ASP.NET to bindable controls like the Gridview so you can present it anyway you like! 

I've added methods below to allow export of the data to Excel as a file streamed XLS file because while the GridView may allow sorting, the XMLDatasource does not and enabling sorting will give you a nice runtime error. I've also added an XPATH expression on the XMLDataSource to allow the XML to be searchable which is useful for extremely large site collection output.
 

<%@ Page Language="C#" EnableEventValidation="false" %>

<%@ import Namespace="System.IO" %>

<%@ import Namespace="System.Data" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

public int iCounter = 0;

public override void VerifyRenderingInServerForm(Control control) { }

protected void Button4_Export_All(object sender, EventArgs e){

GridView1.AllowPaging = false;

GridView1.DataBind();

ExportPageToExcel(GridView1, "pageresults");

}

protected void Button3_Export_Page(object sender, EventArgs e){

ExportPageToExcel(GridView1, "pageresults");

}

public void ExportPageToExcel(GridView grdGridView, string fileName){

Response.Clear();

Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", fileName));

Response.Charset = "";

Response.ContentType = "application/vnd.xls";

StringWriter stringWrite = new StringWriter();

HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

grdGridView.RenderControl(htmlWrite);

Response.Write(stringWrite.ToString());

Response.End();

}

void Page_Load(object sender,EventArgs e){

XmlDataSource1.XPath = "/Sites/Site[contains(@Url,'" + TextBox1.Text.ToString() + "')]";

}

protected void Button1_Click(object sender, EventArgs e){

XmlDataSource1.XPath = "/Sites/Site[contains(@Url,'" + TextBox1.Text.ToString() + "')]";

}

protected void Button2_Click(object sender, EventArgs e){

XmlDataSource1.XPath = "";

TextBox1.Text = "";

}

 

void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e) {

if (e.Row.RowType == DataControlRowType.DataRow){

   iCounter += 1;

}

// actually - we only use this as a counter here

Label1.Text = "Total Records = " + iCounter.ToString();

}

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Site Report</title>

</head>

<body>

<form id="form1" runat="server">

<div>

&nbsp;<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Site Search" />

<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Clear Search" />

<asp:Button ID="Button3" runat="server" OnClick="Button3_Export_Page" Text="Export Page" />&nbsp;

<asp:Button ID="Button4" runat="server" OnClick="Button4_Export_All" Text="Export All" />&nbsp;

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

<hr />

<asp:GridView onrowdatabound="GridView1_RowDataBound" DataSourceID="XmlDataSource1" ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"

CellPadding="5" ForeColor="#333333" AllowSorting="False" EnableViewState="False" >

<Columns>

<asp:BoundField DataField="Url" HeaderText="Site Name" SortExpression="Url" />

<asp:BoundField DataField="Owner" HeaderText="Admin Contact" SortExpression="Owner" />

<asp:BoundField DataField="SecondaryOwner" HeaderText="Secondary Contact" SortExpression="SecondaryOwner" />

<asp:BoundField DataField="ContentDatabase" HeaderText="Content Database" SortExpression="ContentDatabase" />

<asp:BoundField DataField="StorageMaxMB" HeaderText="Storage Max (MB)" SortExpression="StorageMaxMB" />

<asp:BoundField DataField="StorageUsedMB" HeaderText="Storage Used (MB)" SortExpression="StorageUsedMB" />

<asp:BoundField DataField="StorageWarningMB" HeaderText="Storage Warning (MB)" SortExpression="StorageWarningMB" />

</Columns>

<FooterStyle BackColor="#DAE8FD" ForeColor="Navy" Font-Bold="True" />

<RowStyle BackColor="#EFF3FB" />

<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />

<PagerStyle BackColor="#DAE8FD" ForeColor="Navy" HorizontalAlign="Left" />

<HeaderStyle BackColor="#DAE8FD" Font-Bold="True" ForeColor="Navy" HorizontalAlign="Left"/>

<AlternatingRowStyle BackColor="White" />

<EditRowStyle BackColor="#2461BF" />

</asp:GridView>

<!-- the XmlDataSource does not support sorting, so if its enable it on the grid - you'll get a runtime error -->

<!-- The DataFile path listed below will not work in MOSS - you ned to reference a path under the _LAYOUTS directory in the 12 Hive-->

<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/sites.xml" ></asp:XmlDataSource>

</div>

</form>

</body>

</html>

Of course within MOSS it will have the usual MOSS layout wrapped around it.  Still, a simple solution to a potentially complex problem.

10/3/2007 10:40:49 AM (GMT Standard Time, UTC+00:00) #    Comments  |  Trackback

 

All content © 2009, John Timney
On this page
This site
Calendar
<January 2009>
SunMonTueWedThuFriSat
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567
Archives
Sitemap
Blogroll OPML
Talk to Me

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Send mail to the author(s) E-mail