SPSiteDataQuery returns no data when ViewFields property includes a multi-value field

Applies to: Microsoft SharePoint Foundation 2010SharePoint Server 2010

Symptoms


Consider the following scenario:

You have a multi-value taxonomy field called "taxonomy1' in a list. This field can hold multiple values from the taxonomy store. Given this design, if you execute a code [as shown below] that uses SPSiteDataQuery class where the field "taxonomy1" is referenced in the ViewFields property of the SPSiteDataQuery object, you will see no results from this query even though there is data in the "taxonomy1" field.
using (SPSite site = new SPSite(http://<YOUR_SHAREPOINT_SITE_URL>"))
{
SPWeb web = site.OpenWeb();
SPSiteDataQuery query = new SPSiteDataQuery
{
Query = "<Where><Eq><FieldRef Name='taxonomy1/><Value Type='TaxonomyFieldTypeMulti'>Workflows</Value></Eq></Where>",
ViewFields = "<FieldRef Name='Title'/><FieldRef Name='taxonomy1' Type='TaxonomyFieldTypeMulti'/>",
Lists = "<Lists ServerTemplate=\"100\"/>",
Webs = "<Webs Scope=\"SiteCollection\"/>"
};
DataTable dt = web.GetSiteData(query);
if (dt.Rows.Count > 0)
dataGridView1.DataSource = dt;
else
MessageBox.Show("No rows returned");
}

Cause


SharePoint does not support including multi-value fields in the ViewFields property of SPSiteDataQuery object. This applies to the following field types as well:

- LookupMulti - A lookup field that allows multiple values.
- UserMulti - A person and group field that allows multiple values.
- TaxonomyFieldTypeMulti - A taxonomy field that allows multiple values.


Resolution


1. When referencing multi-value fields in ViewFields property of SPSiteDataQuery object, include the "Nullable" attribute and set it to "true" as shown below.
ViewFields = "<FieldRef Name='Title'/><FieldRef Name='taxonomy1' Nullable='true' Type='TaxonomyFieldTypeMulti'/>"
This will ensure that the query returns the correct results, but the value for field "taxonomy1" will be empty. If filtering on multi-value field is what is needed, then include the multi-value field in the <Where/> clause of the query.

2. Use the SPQuery object.