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" /> </asp:GridView> <WSS:SPDataSource runat="server" ID="dsLists" DataSourceMode="ListOfLists" /> <asp:GridView runat="server" ID="grdLists" DataSourceID="dsLists" AutoGenerateColumns="True"> <RowStyle VerticalAlign="Top" /> </asp:GridView>
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" /> </WSS:MenuTemplate>
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"> <Columns> <WSS:SPMenuField HeaderText="Site Title" NavigateUrlFields="__spDefaultUrl" NavigateUrlFormat="{0}" MenuTemplateId="WebMenu" TokenNameAndValueFields="URL=__spUrl,ID=__spID" TextFields="__spTitle" /> <WSS:SPBoundField HeaderText="Site Created" DataField="__spCreated" /> </Columns> </WSS:SPGridView>
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?
September 9, 2008 at 7:39 pm
[…] SPDataSource Mystery Modes: Webs & ListOfLists […]
February 3, 2009 at 1:13 pm
Where are you getting the values __spUrl, __spID, __spTitle and __spCreated From?
Are these hidden Fields or variables you’ve defined?
February 3, 2009 at 3:06 pm
The available fields depends on the DataSourceMode property. For ListItem, List and CrossList modes, the available fields depend on the list columns; for ListOfLists and Webs, a complete list of fields is available here: https://solutionizing.net/spdatasource-fields-for-webs-listsoflists/.
In the case of this SPGridView, the datasource is in Webs mode, with __spUrl returning the SPWeb.Url property, __spTitle returning SPWeb.Title, etc.
Cheers ~
Keith
April 1, 2009 at 10:00 am
I love this stuff, but I have not yet found anyone who can tell me where to put this type of code. I tried putting it on a custom newform.aspx page, but that did not work. I am guessing this goes in a webpart maybe? I just can not find a full example of where to put it.
August 1, 2009 at 11:04 pm
I think there has been a bit of backfilling going on with some MSDN documentation. The following article gives some examples of what the different SPDataSource modes do – http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.spdatasource.datasourcemode.aspx – I could have sworn this wasn’t available 12 months ago….