I’m slowly realizing how powerful the DataView WebPart really is. In this post I’m going to describe how the DVWP can recursively search from a site level to all subsites for documents by creating a DataSource set to “CrossList”. In this mode the WebPart will act like a Content Query WebPart but with all the expressive power of a DVWP. In this example a simple basic working CrossList DVWP will be setup “crawling” for Document Libraries.
In SharePoint designer, insert a DVWP using menu command DataView > Insert Data View
Drag a Document Library onto the DataView and let it populate the default columns
Edit the Markup as follows: Change the DataSource to be CrossList instead of List, and tweak the SelectCommand as shown in Yellow:
<SharePoint:SPDataSource runat="server" DataSourceMode="CrossList" UseInternalName="true" SelectCommand="<Webs Scope='Recursive'></Webs><Lists ServerTemplate='101'></Lists><View><ViewFields><FieldRef Name='Title'/><FieldRef Name='FileDirRef'/></ViewFields><Query><OrderBy><FieldRef Name='Title'/></OrderBy></Query></View>" id="dataformwebpart1"> <SelectParameters> ... </SelectParameters> <DeleteParameters> ... <UpdateParameters> ... </UpdateParameters> <InsertParameters> ... </InsertParameters> </SharePoint:SPDataSource>
Those parts hilited in Red can be deleted.
A Brief explanation of the CAML Query is in order:
<SharePoint:SPDataSource ID="SPDataSource1" runat="server" DataSourceMode="CrossList" UseInternalName="true" SelectCommand="<Webs Scope='Recursive'></Webs> <Lists ServerTemplate='101'></Lists> <View> <ViewFields> <FieldRef Name='Title'/> <FieldRef Name='FileDirRef'/> </ViewFields> <Query> <OrderBy> <FieldRef Name='Title'/> </OrderBy> </Query> </View>" > </SharePoint:SPDataSource>
First the DataSourceMode is set to CrossList to indicate that subsites are to be searched. With this the “Webs Scope” attribute needs to be set to “Recursive”. You can also validate the CAML query by pasting it into the window of U2U CAML Query Builder program and clicking “Execute Query”.
Next we specify that Document Libraries are the Target by specifying ServerTemplate to 101.
In the bulk of the CAML query, we are showing two fields, Title and FileDirRef (the full URL to the document), and ordering the output by the Title field. It is important to note that all the fields specified in the <ViewFields> tag will be exposed to the “Edit Columns” dialog of the DVWP, and stored in its <DataFields> tags.
Define a new parameter WebURL that will take the actual URL to “crawl”:
<parameterbindings> <ParameterBinding Name="WebURL" Location="None" DefaultValue="/sitename/subsitename/"/> ... </parameterbindings>
Specify a value for the parameter WebURL:
<xsl:param name="WebURL">/mysite/AdjustmentBureau/</xsl:param> ...
Add the two columns to show by either editing the XSL of the DVWP, or by using the “Edit Columns” DVWP context menu command:
<td> <xsl:value-of select="@Title"/> </td> <td> <xsl:value-of select="@FileDirRef" /> </td>
Save page and render in browser. It should show a list of documents “crawling” from the root site specified in the datasource. From this vanilla CrossList DVWP you can specialize the columns, query and sorting as required.
Note that there is some performance trade-off with CrossList queries, so consider using a LinkedSource of all the Document Libraries if they are at just one sub-site level or have a static lifespan (i.e. not many new document libraries will be created/deleted at subsites).