Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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).

share|improve this question
1  
Have you tried to add Nullable="True" at FielRef? –  Bargant Jan 9 '12 at 11:57

2 Answers 2

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.

share|improve this answer
    
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. –  HutzelGutzel 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. –  HutzelGutzel 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/… –  Stuart Pegg Apr 17 '12 at 8:38

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

share|improve this answer
    
I'll check if that works, but this would be a dirty solution –  HutzelGutzel 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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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