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.
SharePoint 2010: I need a help. I have an xsllistviewwebpart for a document library. And a filter applied to it. The customer actually needs to view all the documents by default. Currently no items are shown on the default view. Please help.
Safiya:
It sounds like you have a filter applied which screens out all of the items?
M.
Hi Marc
A great article which has made my custom forms much more portable. I’m afraid I have a question though – how do you apply this kind of change to the properties of an XsltListViewWebPart on a form?
If I try to change the ListId or ListName properties of the tag it gets an error so I have to leave it with hardcoded GUIDs.
Thanks
Adrian
Adrian:
There’s more hard coded in XsltListViewWebParts, unfortunately, like the view GUID, the Url to the Allitems view, etc. You’ll need to expereiment with taking those bits and pieces out, but I don’t think you’ll be able to accomplish the same thing as you can with the DVWP.
M.
Apply your filter before you change ListID to ListName. The selectcommand will embody the filter, and you can safely change the ListID to ListName. Or, apply the filter on a test page, then copy the selectcommand from that page to your real page.
Marc, it is worth mentioning that in SP2010, SPD adds the ListID and ListName properties to the WebpartPages:DataFormWebPart tag, and those properties use the list GUID. Simply remove those properties entirely, and substitute the ListDisplayName property, like this: ListDisplayName=”[display name of your list]”. I have also heard it suggested that you change the ViewFlag property of the DataFormWebPart tag to 0 instead of 8, but I do not know whether that is in fact crucial.
As for not being able to apply filters, try applying the filter in an unchanged test page, then copy the selectcommand to your ListName version of the page. I have had success with that.
Keith:
Yes, you have to remove the ListID and ListName properties in the header in SharePoint 2010 and 2013. No need to replace them with anything; you can simply delete.
M.
Great! Do you have any idea why changes in the “Parameters Editor..” on the web part itself don’t work once it’s added to a page Marc? I’ve tried changing the ListName via the web part and nothing happens unless I make the ListName changes to the tag in the data view via SharePoint Designer? That button in the web part seems absolutely redundant!
Joel:
In the default setup, there’s also a parameter called ListID, but that’s a different thing. This trick only works if you edit the elements in the DataSource.
M.
Sorry Marc, that last comment about the Parameters Editor was posted by me but I forgot to put my name and things!
how do i show folder structure in a dataview, after making the scope recurrsive
Sushant:
Making the scope recursive just gets you the data. You’ll need to create some recursive templates in your XSL to emit the structure you want.
M.
Thanks for your blog Marc, I have a list in the root site and using DFWP changed the listid to listname and weburl default value=”/sitecollectionurl/” />. It works fine. I just want to resue DFWP in the subsites. So I changed the default value=”/”/>. It throws an error. Please let me know how can i reuse the webpart in sub sites.
Kanth:
It sounds like the WebURL should be the path to the root of the Site Collection rather than “/”. What you should use is the relative path to the list from the subsite.
M.
Thank you very much for your time. I used relative path and moved DFWP to site Gallery. Now on my sub site I created list with same name as it is there in root site, and when I tried to add DFWP from Gallery on a page it throws List not found error. Could you please let me know where I am doing wrong. I just want to resue DFWP on different subsites with same ListName.
Thanks,
Kanth
Oh, you didn’t mention that you were using the Site Gallery before. I’ve never had much luck with that, as it seems to reconfigure the parameters when it saves the Web Part. I generally just edit the pages and drop the DVWP code into them.
M.