Monday, March 28, 2011

SharePoint 2010 Content Query Web Part and Lookup Field Types

I know the topic of the Content Query Web Part (CQWP) and lookup fields has been covered pretty extensively, but I haven’t found an article anywhere that specifically makes this clear. When including a lookup field in the CommonViewFields property on the CQWP, you have a choice of specifying either “Lookup” or “MultiLookup” as the field type. I haven’t been able to find the latter option documented anywhere.

I’ll use the “Posts” list from the standard Blog site template as an example. It has a multi-value lookup column called “PostCategory” that references the “Categories” list. If you want to include this column in your query and expect to get back a complete dataset, you should set the CommonViewFields property to this:

<property name="CommonViewFields" type="string">PostCategory,MultiLookup</property>

If you don’t specify “MultiLookup” as the field type, any blog posts that have more than one category assigned will be returned in the dataset minus the category that has the lowest internal ID. So if you had some SharePoint-oriented categories like Architecture (ID=1), Administration (ID=2), and Development (ID=3) assigned to a post and you specified “Lookup” as your field type in the CQWP, you’d get back the following value for PostCategory in the returned XML:

PostCategory=”2;#Administration;#3;#Development”

Obviously this is bad. It seems odd that the query behaves this way. I would expect it to only return the first category, not everything else. To fix it, simply make sure you use “MultiLookup” instead. The returned value would then look like this:

PostCategory=”Architecture;#2;#Administration;#3;#Development”

1 comment:

  1. Sir Rob,

    I am looking to add some lookup fields to my CQWP under 'commonviewfields'. I am able to access the main title field coming from the secondary list, but not the subsequest fields I am also passing along. I added them in the CQWP and I get an error when I add that field to the ItemStyle.xsl file. Can you think of why that might be happening?

    ReplyDelete