Data roll-ups are very common when building SharePoint 2010 applications, in my book I dedicated the entire chapter on the subject. When you need a custom data roll-up you don’t always need to create a web part with custom code. One of the ways you can design pretty complex roll-ups is using DataView web part in SharePoint Designer 2010.
In the example below we’ll see how you can aggregate sites below the root site and provide users with links to them.
1. Let’s create a team site with 2 sub-sites under it.
2. Open your team site using SharePoint Designer 2010
3. Check out and open the default page of the site
4. Switch SharePoint designer into the Split View allowing you to see the code and design view at the same time.
5. Just over the top of the existing web parts click the mouse (this is where your data view webpart will appear once we add it in code);
in the code view add the following code:
<SharePoint:SPDataSource
ID="SPDataSource1"
runat="server"
DataSourceMode="Webs"
IncludeHidden="true"
>
<SelectParameters>
<asp:Parameter Name="WebId" DefaultValue="RootWeb" />
</SelectParameters>
</SharePoint:SPDataSource>
Above, we use out-of-the-box SPDataSource object to interact with the SPWeb object on the site. SPDataSource supports few DataSourcesModes:
List - retrieves data from the list
ListItem - retrieves data from a single list item
CrossList – allows to query items in multiple lists in multiple Web sites in the same site collection
ListOfLists - retrieves properties of lists in a specified Web site
Webs - control retrieves properties of sub-sites of the current Web site
Check out more examples on how you can use other modes
Now that we got a hold of the datasource, we need to connect to it and display the list of our sub-sites and links to them. Just below the code we inserted earlier add the following:
<asp:GridView
ID="GridView1"
runat="server"
DataSourceID="SPDataSource1"
AutoGenerateColumns="false">
<Columns>
<asp:HyperLinkField DataTextField="__spTitle" DataNavigateUrlFields="__spUrl" />
</Columns>
</asp:GridView>
Above GridView will take care of rendering all the links with the titles and URLs of our sub sites. Now, when you save and check in the page – DataView will be added to the site as webpart rendering your data
Enjoy!
