Displaying Multi-Select Column Values in CrossList DVWPs
This is one of those things that I’m amazed I haven’t run into before. Perhaps I did in the past and wrote it off to my own lack of understanding. It turns out that this is a known, if rarely mentioned, limitation not only for Data View Web Parts (DVWPs) but also Content Query Web Parts (CQWPs).
You can easily reproduce this issue as follows:
- Create a new Custom List (or any type of list you choose)
- Add a Person or Group column called Project Manager. Do not allow multiple values.
- Add a new item to the list with whatever values you want
- Create a page with a DVWP on it with your list as its DataSource
- Display some columns from the list, including Project Manager
- Add a filter so that you are sure you’ll only get the item(s) you’ve added to your list
- Convert the DVWP to DataSourceMode=”CrossList”
At this point, you should see the item(s) in your DVWP just as you would expect.
Now go back into the list settings and change the Project Manager column settings so that it allows multiple values. Next go back into your page with the DVWP and Refresh the data view. You should now see no items at all.
This simple test should prove that the issue is only the multiple value selection which essentially acts as a “filter” which you didn’t ask for. You can probably pretty easily think of reasons you’d want to display data like this. In my little example, you might have multiple Project Managers in Projects lists in sites across the Site Collection. If you wanted to show all of the projects rolled up somehow, you might well want to display the Project Manager(s).
Unfortunately, in my testing, this works exactly the same in both SharePoint 2007 and 2010.
Once I realized this was what was going on, I turned to the Interwebs and found some old posts from Waldek Mastykarz and others that mentioned the limitation. I could only find a few posts, but when the people who’ve done the posts are as smart as Waldek, I take their word for it – it’s not me this time, it’s a SharePoint limitation.
This is truly one of those “features” which feel an awfully lot like a “bug”.
I did find one trick to at least allow the items to be displayed, even though the multi-select column values will not be displayed. If we add Nullable=”TRUE” to the ViewFields settings in the CAML in the SelectCommand, then we do get the items to display, albeit with blank values for the multi-select columns.
This ends up looking something like this. Note that I have added the Nullable attribute to the Project Manager FieldRef.
<ViewFields><FieldRef Name="Title"/><FieldRef Name="Project_x0020_Manager" Nullable="TRUE"/><FieldRef Name="ID"/><FieldRef Name="PermMask"/></ViewFields>
Now I can see all of the items, but the Project Manager is simply blank. A step forward, but not far enough.
Time for a kludgy fix, don’t you think? Well, I think I’ve got one for you. We can use script and my SPServices jQuery library to “fill in” the values after the page loads. Since we can display the items, but not the values for the multi-select column, we can use the Lists Web Service and specifically GetListItems, to go and grab the items, parse out the multi-select column values, and place them into the DOM where they belong.
This isn’t the type of thing that I like to use jQuery for, really, as it really feels like a kludge. On the other hand, if it plugs a hole in SharePoint’s functionality, maybe that’s not so bad?
To make this work, you’ll want to create good “hooks” in the markup you render for the items in your DVWP or CQWP. I always try to do this, anyway, if I’m going to use script on the page so that my selectors can be very “tight” and efficient.
In the DVWP, I simply add three new attributes for the table detail cell (TD):
- id – This is a unique id for the TD element, which I create by concatenating the string “ProjectManager_” and the current item’s ID. I’ll use these ids to find the empty cells in the DOM.
- ListId – This is the GUID for the list which contains the item. The @ListId “column” contains this value. (This “column” only exists after you switch to CrossList.)
- ItemId – This is the ID for the item itself. We could parse it out from the id above, but it’s easier to store it as its own attribute.
You may not realize that you can create any attributes that you want for HTML elements. They aren’t standards compliant, of course, but by adding your own attributes you can store any values you might need.
<td class="ms-vb" id="ProjectManager_{@ID}" ListId="{@ListId}" ItemId="{@ID}"> <xsl:value-of select="@Project_x0020_Manager" disable-output-escaping="yes"/> </td>
Now that I have markup which makes it pretty easy to both select the right DOM elements as well as the data I need to make the Web Services call, I can use this script:
$(document).ready(function() { var projectManager; // For each empty Project Manager column value... $("td[id^='ProjectManager']").each(function() { // ...call GetListItems to get that item $().SPServices({ operation: "GetListItems", listName: $(this).attr("ListId"), CAMLQuery: "<Query><Where><Eq><FieldRef Name='ID'/><Value Type='Counter'>" + $(this).attr("ItemId") + "</Value></Eq></Where></Query>", async: false, completefunc: function(xData, Status) { // Parse out the Project Manager value projectManager = $(xData.responseXML).find("[nodeName='z:row']").attr("ows_Project_x0020_Manager"); } }); // Create the links and the column value into the DOM $(this).html(userLinks(projectManager)); }); }); function userLinks(columnValue) { var userArray = columnValue.split(";#"); var numUsers = userArray.length / 2; var out=""; for(var i=0; i < numUsers; i++) { out += "<a href='/_layouts/userdisp.aspx?ID=" + userArray[i*2] + "'>" + userArray[(i*2)+1] + "</a>"; out += i < numUsers ? "<br/>" : ""; } return out; }
Using this simple bit of script fills in the values for the Project Manager by getting the right items and plugging the values into the DOM, like so.
I decided to simply show the names as links to the userdisp.aspx page, with each one on a new line. This link will show either the information for that user in the User Information List or their My Site profile, depending on how the environment is configured.
Depending on what your data looks like (how many items you are displaying, how many multi-select columns you have, etc.), there are obviously some inefficiencies in my example, because I’m calling GetListItems once per item. You could also batch your calls together per list to get all of the items from that list, or whatever made sense in your situation.
Finally, if using script like this gives you a bad feeling, then you could try using a third party Web Part like the Bamboo List Rollup Web Part or just develop your own custom Web Part. But it seems that if you’ve gotten this far, you’re probably trying to stick to the Middle Tier, so the script approach might make sense.
References:
- http://blog.mastykarz.nl/inconvenient-content-query-web-part-lookup-fields-multiple-values/ (Waldek Mastykarz)
- http://social.msdn.microsoft.com/Forums/en-US/sharepointcustomization/thread/117b7744-bb26-45d2-80f3-a03ed84e2103/ (MSDN Forums)
- http://vspug.com/michael/2007/06/28/implementing-spsitedataquery-learning-by-doing/ – (Michael Hofer) – “However, up to my knowledge, it is not possible to show a Multivalue-Lookup in the ViewFields! Every attempt to do this will result in zero search results!”
- http://store.bamboosolutions.com/p-32-list-rollup-web-part.aspx (Bamboo Solutions)
17 Comments