Exporting DataForm WebParts across Webs

I recently spent time customizing a DataFormWebPart to show recent changes in our site.  The data source for this was linked across multiple lists and libraries.  When I was developing the web part I was working in the same Web as all of the lists and libraries in the data source.  My idea was that I would develop the web part in this location and then make it available for display anywhere within the site collection.  To my dismay, when I tried to add the web part to a page in a different Web than the one in which it was developed (and all the lists/libraries in the data source lived), it broke.

The problem was that my data source did not contain parameters for WebURL.  I found this out by recreating the web part in a different Web.  I noticed when I looked at my data source in code view that there was another parameter for each list or library: WebURL.  I recognized this parameter from the Lists web service's GetListItems as the place to specify the site relative url to the web in which the list lives if not in the web which you are running the query.

I went back to my original web part and added the WebURL to all the Lists/Libraries in my datasource, and of course it worked like a champ on whichever Web I imported it.

Here's the beginning of the data source before adding the WebURL parameter:

<SharePoint:SPDataSource runat="server" SelectCommand="&lt;View&gt;&lt;Query&gt;&lt;Where&gt;&lt;Gt&gt;&lt;FieldRef Name=&quot;Modified&quot;/&gt;&lt;Value Type=&quot;DateTime&quot;&gt;&lt;Today OffsetDays=&quot;-7&quot;/&gt;&lt;/Value&gt;&lt;/Gt&gt;&lt;/Where&gt;&lt;/Query&gt;&lt;/View&gt;" DataSourceMode="List" UseInternalName="True" InsertCommand="" DeleteCommand="" UpdateCommand="">
 <SelectParameters>
  <asp:Parameter DefaultValue="DD1EA27A-6BB9-440B-B21A-6D52F218F243" Name="ListID"></asp:Parameter>
 </SelectParameters>…

 

Here's what it looks like after adding the WebURL parameter (my list was at the root of the site collection, hence the '/' as the value)

<SharePoint:SPDataSource runat="server" SelectCommand="&lt;View&gt;&lt;Query&gt;&lt;Where&gt;&lt;Gt&gt;&lt;FieldRef Name=&quot;Modified&quot;/&gt;&lt;Value Type=&quot;DateTime&quot;&gt;&lt;Today OffsetDays=&quot;-7&quot;/&gt;&lt;/Value&gt;&lt;/Gt&gt;&lt;/Where&gt;&lt;/Query&gt;&lt;/View&gt;" DataSourceMode="List" UseInternalName="True" InsertCommand="" DeleteCommand="" UpdateCommand="">
 <SelectParameters>
  <asp:Parameter DefaultValue="DD1EA27A-6BB9-440B-B21A-6D52F218F243" Name="ListID"></asp:Parameter>
  <asp:Parameter DefaultValue="/" Name="WebURL"></asp:Parameter>
 </SelectParameters>

In short, if you plan on distributing a web part across webs that makes use of the SPDataSource, ensure that you include the WebURL parameter.

Leave a Reply