SPDataSource Mystery Modes: Webs & ListOfLists

Chris O’Brien is right: SPDataSource is quite handy. However, its documentation leaves something to be desired. In particular, there aren’t any examples (that I can find) of how to use the Webs and ListOfLists SPDataSourceModes. It turns out that the result sets map to a subset of properties (both internal and public) of SPWeb and SPList, respectively—but prepended with “__sp”. An easy way to see all available fields is to bind the data source to an asp:GridView with auto-generated columns:

<WSS:SPDataSource runat="server" ID="dsWebs" DataSourceMode="Webs" />
<asp:GridView runat="server" ID="grdWebs" DataSourceID="dsWebs" AutoGenerateColumns="True">
    <RowStyle VerticalAlign="Top" />
<WSS:SPDataSource runat="server" ID="dsLists" DataSourceMode="ListOfLists" />
<asp:GridView runat="server" ID="grdLists" DataSourceID="dsLists" AutoGenerateColumns="True">
    <RowStyle VerticalAlign="Top" />

For easy reference, I’ve put together a complete list of fields here:
SPDataSource Fields for Webs & ListsOfLists

Note that SharePoint Designer’s live preview of the grid shows extra columns (for Webs: __spAlerts, __spAllProperties, __spAllUsers, etc) that aren’t included in the rendering outside of Designer.

SPDataSourceMode.Webs Example

A list of fields is all well and good, but what can we do with it? Suppose we want an easy way to access our favorite settings pages for our subwebs. A contrived example, perhaps, but it will serve its purpose. We start with our data source:

<WSS:SPDataSource runat="server" ID="dsWebs"
  DataSourceMode="Webs" IncludeHidden="true"/>

Next, we’ll define a MenuTemplate of the site settings shortcuts we want available. %URL% is a token we’ll define in our SPMenuField.

<WSS:MenuTemplate runat="server" ID="WebMenu" CompactMode="true">
  <WSS:MenuItemTemplate runat="server" Text="Create"
    ClientOnClickNavigateUrl="%URL%/_layouts/create.aspx" />
  <WSS:MenuItemTemplate runat="server" Text="Site Settings"
    ClientOnClickNavigateUrl="%URL%/_layouts/settings.aspx" />
  <WSS:MenuSeparatorTemplate runat="server" />
  <WSS:MenuItemTemplate runat="server" Text="People and groups"
    ClientOnClickNavigateUrl="%URL%/_layouts/people.aspx" />
  <WSS:MenuItemTemplate runat="server" Text="Advanced permissions"
    ClientOnClickNavigateUrl="%URL%/_layouts/user.aspx" />
  <WSS:MenuSeparatorTemplate runat="server" />
  <WSS:MenuItemTemplate runat="server" Text="Site libraries and lists"
    ClientOnClickNavigateUrl="%URL%/_layouts/mcontent.aspx" />
  <WSS:MenuItemTemplate runat="server" Text="Sites and workspaces"
    ClientOnClickNavigateUrl="%URL%/_layouts/mngsubwebs.aspx" />
  <WSS:MenuItemTemplate runat="server" Text="Site features"
    ClientOnClickNavigateUrl="%URL%/_layouts/ManageFeatures.aspx" />
 <WSS:MenuItemTemplate runat="server" Text="Delete this site"
    ClientOnClickNavigateUrl="%URL%/_layouts/deleteweb.aspx" />

And finally, a simple SPGridView with a link and menu for our site, with an extra column just for good measure:

<WSS:SPGridView runat="server" ID="spGrdWebs"
  DataSourceID="dsWebs" AutoGenerateColumns="false">
    <WSS:SPMenuField HeaderText="Site Title"
      TextFields="__spTitle" />
    <WSS:SPBoundField HeaderText="Site Created"
      DataField="__spCreated" />

And our final result will look something like this:

So we’ve seen how to use SPDataSource in Webs mode, plus we have a code-free example of the often-overlooked SPMenuField. For what else could the Webs and ListOfLists modes be used?