Replacing ListIDs with ListNames in Data View Web Parts

This is another idea that I was certain I’d posted about long ago, but I can’t seem to find.  It’s especially useful if you are developing Data View Web Parts (DVWPs) in one environment and then porting them to another on any sort of frequent basis.

When you create a Data Source for a SharePoint list, you end up with some code that looks roughly like this by default (This is a code snippet from a DVWP in WSS and I’ve added spacing to make it more readable.):

<DataSources>
    <SharePoint:SPDataSource runat="server" DataSourceMode="List" UseInternalName="true" selectcommand="<View></View>" id="dataformwebpart1">
        <SelectParameters>
            <asp:Parameter Name="ListID" DefaultValue="6335D0C8-3089-435F-80C4-12850D5CB7D3"/>
        </SelectParameters>
        <DeleteParameters>
            <asp:Parameter Name="ListID" DefaultValue="6335D0C8-3089-435F-80C4-12850D5CB7D3"/>
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="ListID" DefaultValue="6335D0C8-3089-435F-80C4-12850D5CB7D3"/>
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="ListID" DefaultValue="6335D0C8-3089-435F-80C4-12850D5CB7D3"/>
        </InsertParameters>
    </SharePoint:SPDataSource>
</DataSources>

Alternatively, you may see a slightly different structure in MOSS and/or if you have chosen a list in a different site (smaller snippet):

<SelectParameters>
    <WebPartPages:DataFormParameter Name="ListID" ParameterKey="ListID" PropertyName="ParameterValues" DefaultValue="BCD0B397-2550-4937-892A-64370C87913E" />
    <WebPartPages:DataFormParameter Name="WebURL" ParameterKey="WebURL" PropertyName="ParameterValues" DefaultValue="/" />
</SelectParameters>

As you can see, in both cases the list is referred to by its GUID.  This is great on one level because the reference stays intact if you rename the list.  However, if you want to move your DVWP code to another environment which refers to the same list, it will throw an error because the list GUID is different.

So rather than changing the ListID each time, replace the occurrences of ListID with ListName.  This lets you refer to the list by its obvious name rather than the more obscure GUID:

<DataSources>
    <SharePoint:SPDataSource runat="server" DataSourceMode="List" UseInternalName="true" selectcommand="<View></View>" id="dataformwebpart1">
        <SelectParameters>
            <asp:Parameter Name="ListName" DefaultValue="My List Name"/>
        </SelectParameters>
    </SharePoint:SPDataSource>
</DataSources>

or

<SelectParameters>
    <WebPartPages:DataFormParameter Name="ListName" ParameterKey="ListName" PropertyName="ParameterValues" DefaultValue="My Other List Name" />
    <WebPartPages:DataFormParameter Name="WebURL" ParameterKey="WebURL" PropertyName="ParameterValues" DefaultValue="/" />
</SelectParameters>

This makes your DVWP code portable to other environments where there is a list in the same location with the same name, like a DEV/Staging/PROD construct.

Note that I’ve also removed the Delete, Update, and Insert sections above.  If your DVWP is just displaying the contents of a list (which is probably the most common use of a DVWP), you don’t need the Delete, Update, and Insert sections.

Technorati tags: , , ,