Thursday, March 14, 2013

SharePoint 2010 CAML Query Nuance In C#

Whilst playing around in SharePoint with pulling data from lists (Which requires a wonderfully syntactically obscene language known as CAML Query), I found that my query simply refused to filter out data.

string camlQuery = @"
   <Query>   
      <Where>
         <Eq>
            <FieldRef Name = 'Title' />
            <Value Type='Text'>" + chosenIndex + @"</Value>
         </Eq>
      </Where>
   </Query>";
SPListItemCollection items = myList.GetItems(camlQuery);

Seems simple, enough - Right?

Not quite...

Turns out, if you include the <Query> tags in your CAML Query, the entire thing gets blatantly ignored! No errors - It just selects everything o_O

To fix this problem, simply remove the <Query></Query> tags from your statement, and everything will work as planned...

Note: Thanks to manoli.net for the formatting code :D

No comments :

Post a Comment