4

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/">
       <Where>
        <Or>    
         <Contains><FieldRef Name='CustomField' /><Value Type='Text'>Test</Value></Contains>
         <Contains><FieldRef Name='Title' /><Value Type='Text'>Test</Value></Contains>
        </Or>
      </Where>          
    </Query>

EDIT

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
  • 1
    Have you tried to add Nullable="True" at FielRef?
    – Bargant
    Commented Jan 9, 2012 at 11:57

2 Answers 2

8

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.

5
  • 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
    Commented Jan 9, 2012 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) Commented Jan 9, 2012 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
    Commented Jan 10, 2012 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
    Commented Apr 17, 2012 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
    Commented Mar 19, 2021 at 11:14
1

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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