I'm using SPSiteDataQuery to fetch list contents from 2 different lists.

  • List A) has the fields CustomField and Title
  • List B) has only Title

Is it possible to build a (filter) query with nonexistent fields?

The aim is, to use one query for the 2 lists and handle everything with 1 SPSiteDataQuery. The query in this manner, throws an exception on list B)

     <Query xmlns="http://schemas.microsoft.com/sharepoint/soap/">
         <Contains><FieldRef Name='CustomField' /><Value Type='Text'>Test</Value></Contains>
         <Contains><FieldRef Name='Title' /><Value Type='Text'>Test</Value></Contains>


Thanks for your suggestions!

But setting nullable, doesn't work for me, although it sounds like the correct way to do it. I will use one SPSiteDataQuery now for each of the two lists and merge the results (I'll also check SPQuery against it for performance issues).

  • 1
    Have you tried to add Nullable="True" at FielRef? – Bargant Jan 9 '12 at 11:57

You should use Nullable attribute in ViewFields property for fields, which are expected to exist only in few lists which are mentioned in the ViewFields.

Example code:

query.ViewFields = "<FieldRef Name='Title' Type='Text'/>";
query.ViewFields += "<FieldRef Name='CustomField' Type='Text' Nullable='TRUE' />";

Setting the Nullable attribute to TRUE causes an empty value to be returned for lists that do not include the "CustomField" column, so you should be able to use this field in your CAML query.

Also, probably you will have to use the same attribute (Nullable="TRUE") in the query itself, every time you mention the "CustomField" column.

  • Unfortunately that didn't work. Added Nullable for the CustomField to ViewFields and/or the Query doesn't work. Also firing a query with a non existent field via "Stramit CAML Viewer" throws an exception. – Tom Bola Jan 9 '12 at 15:09
  • That's really bad news. Although I haven't tried this code myself, several MSDN articles I've read provide straight examples of doing this. Can you show us the exception message and its stack trace? (you can add this information to your question) – Andrey Markeev Jan 9 '12 at 16:55
  • The exception only occurs in the CAML Viewer tool and is not helpful. It only prints "WebserviceException". In the application there are no results returned when querying nonexistent fields. – Tom Bola Jan 10 '12 at 8:05
  • It could be a capitalisation issue: True instead of TRUE, as the examples I've seen have it in Pascal case: msdn.microsoft.com/en-us/library/aa981241.aspx bryansgeekspeak.com/2008/06/… – Stu Pegg Apr 17 '12 at 8:38
  • Nullable='True' does the trick but you also have to set a Type attribute, like Type='Text' or Type='URL' (all available types can be seen in the table here: docs.microsoft.com/en-us/sharepoint/dev/schema/…) – Evariste Mar 19 at 11:14

Could you add "CustomField" has a hidden, non-mandatory field to ListB?

  • I'll check if that works, but this would be a dirty solution – Tom Bola Jan 9 '12 at 11:55
  • 1
    Indeed. You might only consider it if you don't get a better answer. – Howard Jan 9 '12 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.