Recursive Data View Web Part…
Recently I had to give a hand to one of my friend to create a recursive DVWP. What this web part should do is to query all the sub sites including the root site, and retrieve all the Pages resides inside the Pages libraries. Also it should filter the retrieved pages for a given content type. Previously I had done whole lot of DVWP, but never wanted to create a recursive DVWP.
So how to achieve this,
1) Open up SharePoint Designer and create your DVWP pointing to the root site collection, Pages library.
2) Do the necessary formatting and other stuff (Paging, Sorting).
3) As per my requirements I had to filter the pages based on a content type, so I added the content type filter next. (filter by Article Page content type)
3) Now here comes the tweaks that you will need to do manually.
All below modifications are done to the data source section of the DVWP.
a) Search for the DataSourceMode and set its value to CrossList
b) Copy the select command and decode the query using an HTML decoder. (you can find plenty of online HTML decoders)
You will see a query similar to below image.
c) You have to modify the above query with some new sections like this.
The changes you need to make are:
i) Add the new Webs element with the attribute Scope set to Recursive.
ii) Add new View Section with all view fields that will be used in this DVWP.
iii) Specify the List Template ID of the list type, that you will be querying data for. ( in this scenario its pages library and the pages library template id is 850)
iv) Remove the line breaks and get the query in one line.
v) Get the query HTML encoded.
vi) Replace the SelectCommand of the DVWP data source with this new encoded query text.
SelectCommand attribute can be used with lower case (selectcommand) inside the DVWP data source section. Both will work fine.
When replacing the query in the DVWP, query should be in one line, if not the DVWP will fail.
Make sure all the Fields referred in the DVWP are specified in the ViewFields section in the select command.
That’s all about it Now your DVWP should recursively browse through the sub sites in the site collection and retrieve all the pages in the pages library, that has the Article Page Content type.
Things can be done to clean the code:
Because we are querying though all the sub sites, and we have already specified the list template that we are looking for in the select command itself, we can remove the ListID select parameter. (its no longer used within the web part)