Replacing ListIDs with ListNames in Data View Web Parts

2 minute read

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: , , ,
Advertisements

68 Comments

  1. Hi,

    I ran into a scenario which I have more than 200 pages (with DVWP) where the GUIDs need to be updated or changed to the ListName. Do I need to update them page by page? Is there a tool other than can SPD to speed thing up?

    Your advice is greatly appreciated,
    Alex

    Reply
    • Alex:

      If you’ve already go all 200 of these pages set up and working, do you really need to change to ListNames? In any case, I haven’t ever needed to do such a large set of changes. I almost always switch to the ListName as I’m building the DVWPs. I’d think that you might be able to script it somehow, but it may take just as much time getting someone to figure that out as it would to just do it manually.

      M.

      Reply
  2. After getting errors in SPD 2010, I found out it was caused by me also converting the ListID settings of the DataFormWebPart itself (near the top). Once I left those alone, the changes worked fine. A couple of other things:

    Filtering can still be done with XSLT if needed.

    In this thread, it is suggested that REST services should be used for this kind of cross-site access (and can in fact be used right on up to cross-farm access):
    http://social.technet.microsoft.com/Forums/en-CA/sharepoint2010customization/thread/9fa395f7-b46e-4689-9bda-c5d1e5bbe92c

    I still haven’t gotten REST services to work for me though:
    http://social.technet.microsoft.com/Forums/en-US/sharepoint2010general/thread/158bed2e-ca03-4806-9644-a1e7685fad33

    Reply
      • Sorry for not being clear. What I meant was that when you drop a DVWP in SPD based on a list, there are several ListID references outside of the datasource, up near the start of the webpart specification. I was changing all of those too and getting errors as a result. When I changed just those in the datasource section as you showed, it worked fine. I’m not really sure what purpose the ones up top serve since changing it just in the datasource seems to work fine. Also, if you delete ones like ListID up top, after you save them with SPD2010 they reappear (voodoo), not sure what’s up with that.

        Reply
        • Interesting. There’s an attribute called ‘listname’ added to the WebPartPages:DataFormWebPart in SP2010. I just experimented and simply removed it, which didn’t seem to cause any issues. However, as you pointed out, setting it to the name of the list caused an error. In my case SPD didn’t replace it after I removed it.

          M.

          Reply
  3. Hi..This is working fine with MOSS. however, i am not able to achieve ListName instead ListID in shaerpoint 2010. Please help me with some solution.

    Please let me know what changes i need to do.

    Reply
    • Raj:

      This works exactly the same in SharePoint 2010. (There’s some discussion of this in the comments here already.) Be sure that you are making the change in the DataSources section only.

      M.

      Reply
  4. Hi Marc,
    I know this is a bit of an older post, but I had to replace all of my List GUID with their appropriate List Name – The tips above helped out greatly, but I am now stuck while trying to use the owssvr.dll.
    It seems that I cannot pass a listname to the dll – Do you know of a way to obtain the list GUID from its list Name?

    Thanks for the help

    Reply
  5. Hi Mark,

    I commented on another one of your posts but the question may belong here as well. Will the technique described in your blog post above work with DVWP that display “joined” information from more than one list?

    Thank you for the information on your blog posts and being such a great resource to the SharePoint community.

    Reply
  6. Hi Marc

    I’m trying to show a root site level list (http://servername) on a master page in a subsite collection (http://servername/site/mysite), but setting WebURL parameter up to ‘{sitecollectionroot}’ it always gets http://servername/site/mysite and setting up WebURL=’/’ reports a ‘Url address not valid.’
    I’ve also tried WebURL=’/servername’ which gets a wrong url: /site/mysite/servername.

    I’ve followed steps to configure ListName parameter well.

    Any ideas?, thanx in advance.

    Reply

Have a thought or opinion?