Mavention Blog > Februari 2009

SPSiteDataQuery not returning any data

The QPSiteDataQuery object enables you to describe a query performed across multiple lists, which may be located in multiple web sites in the same Web site collection.
One of the major features is that all queries that are performed trough this object are cached. This means that when you perform the query multiple times on the same data you’ll experience better performance than using a object that does not use data caching.
Like most of the WSS\MOSS query objects, you’ll have to configure the query object by using CAML.
Recently I got a question why the SPSiteDataQuery object was not returning data, although there were enough records that would match the query.
For example: Suppose you have a simple custom list in WSS/MOSS that contains just two fields
1.       Title
2.       Email address
This list contains the following records:
·         User A; user_a@domain.com
·         User B; user_b@domain.com
·         User C;
As you can see in the list above, User C has no filled in email address.
To query this list you could configure the SPSiteDataQuery object as below:
SPList list = web.Lists["Testlist"];
SPSiteDataQuery query = new SPSiteDataQuery();
query.Lists = string.Format("<Lists><List ID='{0}'/></Lists>", list.ID);
query.ViewFields = "<FieldRef Name='Email' />";
query.RowLimit = 25;
 
As you can see I defined the ViewFields property to display the email field. But what will happen when the field in question has no data entered by the user, as User C in our example? When the query hits a field with no data entered by the user it will abort the query completely and will not return any records. Why not just skipping the records that do not match the query? I don’t know, but the good news is that you can configure the query to display all results even if a so called ‘null-value’ is being encountered.
How to accomplish this? Just change the ViewFields property by adding a ‘nullable=true’ attribute to the CAML syntax.
query.ViewFields = "<FieldRef Name='Email' Nullable='True' />";

Et voila. Now you have the results that you expected in the first place.

Ciao,
Niels

Posted: 18-2-2009 11:45:16 by Niels Loup | with 2 comments


Commentaren
surya
Hi Niels
Have you tried fetching data from a list and a doc lib simultaneously ? I looked into the documentation and it seems it is not possible. The base type should be either 0 or 1 but how can I specifiy to take data from both ?
Any help would be greatly appreciated.
Thanks
30-3-2009 13:08:00
Rapporteer misbruik
TheKid
Surya, do not understand what you mean ???
You should query them the one after the other. Then merge the results of the 2 queries into one table. But this only works when the field names of the results match with each other.
1-5-2009 15:08:30
Rapporteer misbruik
Laat commentaar achter



 Security code