Easily Making the Data Form Web Part reusable

There are plenty of examples out there of the many capabilities of the so called "Swiss Army" Web Part, the Data Form Web Part.  Although a lot of developers know about the capabilities, many of them dismiss this Web Part as a one-off, not a versatile re-useable Web Part.  I ran into such a situation recently on a project where one of the developers wanted to write a custom Web Part to display some data that was easily done by the Data Form Web Part.  His reason for writing a custom Web Part was because the requirement was for this Web Part to be re-usable across site collections.  The goal of this post is to show how to easily transform a one-off Data Form Web Part into a re-useable web part, with no code.  Let's get started...

The first thing you will want to do is build your Data Form Web Part.  If you are simply accessing an external data source such as a web service then you can skip the next step.  You can see an example of using the Flickr web services here.  There are plenty more examples of how to use the Data Form Web Part over on the SharePoint Designer Team Blog.

If your data source is SharePoint content such as a list or library you will need to make sure that you update your SPDataSource to be Site Collection agnostic.  The most important part of this is removing the "ListID" paramter from the paramter list.  Here is an example of what was generated by SharePoint designer and what it looked like after I modified it.

SharePoint Designer

 <WebPartPages:DataFormWebPart runat="server" IsIncluded="True" FrameType="None" NoDefaultStyle="TRUE" ViewFlag="0" Title="Important Phone Numbers" ListName="{1EC8059A-4D81-4AD9-88D2-56B47F6BDCF2}" Default="FALSE" 
 DisplayName="Important Phone Numbers" __markuptype="vsattributemarkup" __WebPartId="{69639AAA-B062-42E5-9C34-11700E80E785}" id="g_69639aaa_b062_42e5_9c34_11700e80e785">
    <DataSources>
        <SharePoint:SPDataSource 
            runat="server" 
            DataSourceMode="List" 
            UseInternalName="true" 
            selectcommand="&lt;View&gt;&lt;/View&gt;" id="Important_x0020_Phone_x0020_Numbers1">
            <SelectParameters>
                <WebPartPages:DataFormParameter Name="ListID" ParameterKey="ListID" PropertyName="ParameterValues" DefaultValue="1EC8059A-4D81-4AD9-88D2-56B47F6BDCF2"/>
            </SelectParameters>
            <DeleteParameters>
                <WebPartPages:DataFormParameter Name="ListID" ParameterKey="ListID" PropertyName="ParameterValues" DefaultValue="1EC8059A-4D81-4AD9-88D2-56B47F6BDCF2"/>
            </DeleteParameters>
            <UpdateParameters>
                <WebPartPages:DataFormParameter Name="ListID" ParameterKey="ListID" PropertyName="ParameterValues" DefaultValue="1EC8059A-4D81-4AD9-88D2-56B47F6BDCF2"/>
            </UpdateParameters>
            <InsertParameters>
                <WebPartPages:DataFormParameter Name="ListID" ParameterKey="ListID" PropertyName="ParameterValues" DefaultValue="1EC8059A-4D81-4AD9-88D2-56B47F6BDCF2"/>
            </InsertParameters>
        </SharePoint:SPDataSource>
    </DataSources>

After Modifications

 <WebPartPages:DataFormWebPart runat="server" IsIncluded="True" FrameType="None" NoDefaultStyle="TRUE" ViewFlag="0" Title="Important Phone Numbers" ListName="{1EC8059A-4D81-4AD9-88D2-56B47F6BDCF2}" Default="FALSE" 
 DisplayName="Important Phone Numbers" __markuptype="vsattributemarkup" __WebPartId="{69639AAA-B062-42E5-9C34-11700E80E785}" id="g_69639aaa_b062_42e5_9c34_11700e80e785">
    <DataSources>
        <SharePoint:SPDataSource 
            runat="server" 
            DataSourceMode="List" 
            UseInternalName="true" 
            selectcommand="&lt;View&gt;&lt;/View&gt;" id="Important_x0020_Phone_x0020_Numbers1">
            <SelectParameters>
                <WebPartPages:DataFormParameter Name="ListName" ParameterKey="ListName" PropertyName="ParameterValues" DefaultValue="Important Phone Numbers" />
                <asp:Parameter runat="server" Name="WebUrl" DefaultValue="{sitecollectionroot}"/>
                <asp:Parameter Name="MaximumRows" DefaultValue="2"/>
            </SelectParameters>
        </SharePoint:SPDataSource>
    </DataSources>
    
 Since we are just displaying data I removed the Insert, Update, and Delete parameter collections.  The important modification I made to the "SelectParameters" parameter collection was to change the "ListID" parameter to "ListName".  
 This will get the list by name instead of GUID, which is important if you are going to reuse this Web Part in another Site Collection.  At this point you can continue updating your DFWP by modifying the XSL or adding some parameters from the querystring
 or other controls on the page, there are plenty examples out there of how to do this, I won't go into detail here.
  
 Next we need to create a reusable web part out of this DFWP.  To do this we will place the DFWP in a Web Part Zone and export through the standard SharePoint browser interface.
 Here is an example of my markup:
 <WebPartPages:WebPartZone 
    runat="server" 
    AllowPersonalization="false" 
    ID="TopZone" 
    Title="Top Zone" 
    Orientation="Horizontal">
<ZoneTemplate>

<WebPartPages:DataFormWebPart runat="server" IsIncluded="True" FrameType="None" NoDefaultStyle="TRUE" ViewFlag="0" Title="Important Phone Numbers" ListName="{1EC8059A-4D81-4AD9-88D2-56B47F6BDCF2}" Default="FALSE" DisplayName="Important Phone Numbers" __markuptype="vsattributemarkup" __WebPartId="{69639AAA-B062-42E5-9C34-11700E80E785}" id="g_69639aaa_b062_42e5_9c34_11700e80e785" partorder="1">

....[omitted for brevity]

</WebPartPages:DataFormWebPart>

</ZoneTemplate>
</WebPartPages:WebPartZone>
  
 Now when I go to my page I can edit the page, and select export.  

image

 This will produce a .webpart file that I can then package in my features or use in other web part galleries to create an easily reusable DFWP.