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.
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
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.
Are you able to use this technique with Inserting Data Sources? I’ve tried, but I keep getting errors. Have you done something like that before?
Matt:
This ought to work with all four operations.
M.
This does work… Must’ve been an ID10T error.
Thanx alot sir………
it helped me alot
a million thanx
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
Clark:
I’m not sure what you mean by “near the top”. This trick works if you make the change in the DataSources section.
Note this workaround for the missing “Connect to another library…” option in 2010.
http://sympmarc.com/2010/11/02/using-a-datasource-in-a-data-view-web-part-dvwp-in-a-different-site-in-sharepoint-designer-2010/
M.
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.
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.
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.
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.
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
TeckniX:
If you just need to figure out the ListID again for a particular list, go to the List Settings and the GUID will be on the URL.
M.
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.
Thanks, Marc. I used this today to save me some time. =)
Excellent, Clayton!
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.
JoseLuis:
DVWPs (as is true of most other Web Parts) can only access data within the same Site Collection. There’s no trick around it.
M.