Reuse DataForm Web Part, change ListId to ListName, and scrub GUIDs

Intro

I know there are quite a few posts out there on this, but i’m writing this here for myself as a rollup of the information and highlighting what needs to be done in order for this to work perfectly.

For those not familiar with the issue let me re-hash it quickly. When you create a Data View Web Part (DVWP) or New/Display/Edit form in SharePoint Designer you’ll be directly binding those web parts to your list. If you look at the code you’ll see the list GUIDs below.

<WebPartPages:DataFormWebPart runat=”server” EnableOriginalValue=”False” DisplayName=”My Links” ViewFlag=”8″ ViewContentTypeId=”” Default=”FALSE” ListUrl=”” ListDisplayName=”” ListName=”{62F19BE4-6465-4293-B580-36C49EC8F35B}” ListId=”62f19be4-6465-4293-b580-36c49ec8f35b” PageType=”PAGE_NEWFORM”………………………

Copying this to another site collection or environment won’t work unless your lists have exactly the same GUID. Basically these web parts are non-reusable. Well there is a way to move them.

Two articles that I ALWAYS refer too – but they don’t do it partially or not as cleanly as I want.

http://sympmarc.com/2008/12/16/replacing-listids-with-listnames-in-data-view-web-parts/

http://blogs.msdn.com/b/joshuag/archive/2008/11/10/easily-making-the-data-form-web-part-reusable.aspx

There are a few places you need to rename the ListId from in order for this to work.

  1. The <DataFormWebPart>
  2. The <DataSources>
  3. The <ParameterBindings>
  4. The <XSL:StyleSheet><Params>

Step 1 – DataFormWebPart

This is an original:

<WebPartPages:DataFormWebPart runat=”server” EnableOriginalValue=”False” DisplayName=”My Links” ViewFlag=”8″ ViewContentTypeId=””
Default=”FALSE” ListUrl=”” ListDisplayName=”” ListName=”{62F19BE4-6465-4293-B580-36C49EC8F35B}” ListId=”62f19be4-6465-4293-b580-36c49ec8f35b”
    PageType=”PAGE_NEWFORM” PageSize=”-1″ UseSQLDataSourcePaging=”True” DataSourceID=”” ShowWithSampleData=”False” AsyncRefresh=”False”
ManualRefresh=”False” AutoRefresh=”False” AutoRefreshInterval=”60″ NoDefaultStyle=”TRUE” InitialAsyncDataFetch=”False” Title=”My Links”
FrameType=”None” SuppressWebPartChrome=”False” Description=”” IsIncluded=”True” PartOrder=”2″ FrameState=”Normal” AllowRemove=”True”
AllowZoneChange=”True” AllowMinimize=”True” AllowConnect=”True” AllowEdit=”True” AllowHide=”True” IsVisible=”True” DetailLink=”” HelpLink=””
HelpMode=”Modeless” Dir=”Default” PartImageSmall=”” MissingAssembly=”Cannot import this Web Part.” PartImageLarge=”” IsIncludedFilter=””
ExportControlledProperties=”True” ConnectionID=”00000000-0000-0000-0000-000000000000″ ID=”g_b881fc6c_50db_4108_825a_a5b252da499d”
ChromeType=”None” ExportMode=”All” __MarkupType=”vsattributemarkup” __WebPartId=”{B881FC6C-50DB-4108-825A-A5B252DA499D}” __AllowXSLTEditing=”true”
WebPart=”true” Height=”” Width=””>

Remove ListId and ListName completely.

Step 2 – Data Sources

These are the original data sources. I ALWAYS remove the one’s I’m not using. If your DVWP or List Form does not use the delete then remove it. In my current working example I’m working with an add new item form so all I need it the <InsertParameter>

<DataSources>
<SharePoint:SPDataSource runat=”server” DataSourceMode=”ListItem” SelectCommand=”&lt;View&gt;&lt;Query&gt;&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef Name=&quot;ContentType&quot;/&gt;&lt;Value Type=&quot;Text&quot;&gt;Link&lt;/Value&gt;&lt;/Eq&gt;&lt;/Where&gt;&lt;/Query&gt;&lt;/View&gt;” UseInternalName=”True” UseServerDataFormat=”True” ID=”My_x0020_Links2″>
<SelectParameters>
<WebPartPages:DataFormParameter ParameterKey=”ListID” PropertyName=”ParameterValues” DefaultValue=”{62F19BE4-6465-4293-B580-36C49EC8F35B}” Name=”ListID”/>
<WebPartPages:DataFormParameter ParameterKey=”MaximumRows” PropertyName=”ParameterValues” DefaultValue=”10″ Name=”MaximumRows”/>
</SelectParameters>
<UpdateParameters>
<WebPartPages:DataFormParameter ParameterKey=”ListItemId” PropertyName=”ParameterValues” DefaultValue=”0″ Name=”ListItemId”/>
<WebPartPages:DataFormParameter ParameterKey=”ListID” PropertyName=”ParameterValues” DefaultValue=”{62F19BE4-6465-4293-B580-36C49EC8F35B}” Name=”ListID”/>
</UpdateParameters>
<InsertParameters>
<WebPartPages:DataFormParameter ParameterKey=”ListItemId” PropertyName=”ParameterValues” DefaultValue=”0″ Name=”ListItemId”/>
<WebPartPages:DataFormParameter ParameterKey=”ListID” PropertyName=”ParameterValues” DefaultValue=”{62F19BE4-6465-4293-B580-36C49EC8F35B}” Name=”ListID”/>
</InsertParameters>
<DeleteParameters>
<WebPartPages:DataFormParameter ParameterKey=”ListItemId” PropertyName=”ParameterValues” DefaultValue=”0″ Name=”ListItemId”/>
<WebPartPages:DataFormParameter ParameterKey=”ListID” PropertyName=”ParameterValues” DefaultValue=”{62F19BE4-6465-4293-B580-36C49EC8F35B}” Name=”ListID”/>
</DeleteParameters>
</SharePoint:SPDataSource>
</DataSources>

My updated XML below. I’ve changed the DataFormParameter, Name & ParameterKey to “ListName” and in DefaultValue I’m using the “Links” as the internal name of my list. I’m also specifying the an asp:Parameter of the “WebUrl” so that this can connect to the proper list. This {sitecollectionroot} was pulled from this article – http://blogs.msdn.com/b/joshuag/archive/2008/11/10/easily-making-the-data-form-web-part-reusable.aspx

<DataSources>
<SharePoint:SPDataSource runat=”server” DataSourceMode=”ListItem” UseInternalName=”true” UseServerDataFormat=”True” selectcommand=”&lt;View&gt;&lt;Query&gt;&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef Name=&quot;ContentType&quot;/&gt;&lt;Value Type=&quot;Text&quot;&gt;Link&lt;/Value&gt;&lt;/Eq&gt;&lt;/Where&gt;&lt;/Query&gt;&lt;/View&gt;” id=”My_x0020_Links1″>
<InsertParameters>
<WebPartPages:DataFormParameter Name=”ListItemId” ParameterKey=”ListItemId” PropertyName=”ParameterValues” DefaultValue=”0″/>
<WebPartPages:DataFormParameter Name=”ListName” ParameterKey=”ListName” PropertyName=”ParameterValues” DefaultValue=”Links”/>
<asp:Parameter runat=”server” Name=”WebUrl” DefaultValue=”{sitecollectionroot}” />
        </InsertParameters>
</SharePoint:SPDataSource>
</DataSources>

Step 3 – Parameter Bindings

Your ParameterBindings should normally look similar to this. I just completely remove the ListId line as it’s not needed in MY xslt

<ParameterBindings>
<ParameterBinding Name=”dvt_apos” Location=”Postback;Connection”/>
<ParameterBinding Name=”ManualRefresh” Location=”WPProperty[ManualRefresh]”/>
<ParameterBinding Name=”UserID” Location=”CAMLVariable” DefaultValue=”CurrentUserName”/>
<ParameterBinding Name=”Today” Location=”CAMLVariable” DefaultValue=”CurrentDate”/>
<ParameterBinding Name=”ListItemId” Location=”QueryString(ID)” DefaultValue=”0″/>
    <ParameterBinding Name=”ListID” Location=”None” DefaultValue=”{62F19BE4-6465-4293-B580-36C49EC8F35B}”/>
<ParameterBinding Name=”MaximumRows” Location=”None” DefaultValue=”10″/>
</ParameterBindings>

Step 4 – XSL Style Sheet Params

In the very top of the XSL:StyleSheet you see a list of xsl:params. I just remove the ListID param as I’m not normally using it in my XSL.

<xsl:stylesheet …………[cut for brevity] ……………………>
<xsl:output method=”html” indent=”no”/>
<xsl:decimal-format NaN=””/>
<xsl:param name=”dvt_apos”>&apos;</xsl:param>
<xsl:param name=”ManualRefresh”/>
<xsl:param name=”ListItemId”>0</xsl:param>
<xsl:param name=”ListID”>{62F19BE4-6465-4293-B580-36C49EC8F35B}</xsl:param>
            <xsl:param name=”MaximumRows”>10</xsl:param>
<xsl:param name=”FavoriteLink” />
<xsl:variable name=”dvt_1_automode”>0</xsl:variable>

Finished…

Each step of the way you can save and test. When you are finished you should be GUID free.

Conclusion

This may not be a silver bullet for all scenarios of using the DVWP or some of the list form web part through designer. This is what I’ve found to work for me time after time. There may be cases when you need the ListID in the XSLT and you’d want to keep it there and replace the ListID with ListName there.

If you find this not to work please contact me and we’ll sort it out and keep this post current.

Advertisements

7 thoughts on “Reuse DataForm Web Part, change ListId to ListName, and scrub GUIDs

  1. Thomas:

    Great that you pulled all of this together. It’s changed a bit over time. For instance, we didn’t need to worry about Step 1 in SharePoint 2007.

    Also note that Steps 3 & 4 aren’t required *unless* you use the ListID value in your XSL. Since they are just parameters, the value only matters if you use it. However, cleaning out anything that isn’t useful is an excellent rule, especially as you do in Step 2.

    M.

  2. whoah this weblog is fantastic i really like reading your posts. Stay up the good work! You recognize, lots of people are looking round for this info, you can aid them greatly.

  3. Hi,

    I am trying to display data from shared documents library using dataview webpart. I replaced ‘list id’ with ‘list name’ and removed the guid as suggested. I am trying to upload this webpart file to a different web application. Here i am getting ‘The operation could not be completed because the item was removed from the gallery.’

    The destination web application has shared documents at root level. Did i miss anything here? Please suggest.

    • the dvwp needs to be added to the same site as where the list is. If not there is an additional property that for the web guid that you need to add. I don’t have instructions for doing that. It needs to be exactly like it was on the site you created it on if you plan to move it

Comments are closed.