Search code examples
sharepointcaml

Caml query returns wrong rows


I've an issue with a caml query - to select a sharepoint item where the field "Title" is of a specific value...

  Dim camlQuery As New CamlQuery()
  camlQuery.ViewXml = "<Query><where><Contains><FieldRef Name='Title'/><Value Type='Text'>" & orderno & "</value></Contains></where></Query>"

Problem is this returns all the items in the list. Orderno will be a guid, so it is unique. I've read a few questions on here that have had a few suggestions but nothing has worked.

I've tried replacing

  <eq> 

with

  <contains>

But that's no use. The start of the query used to read

  <View><Query>      

And I've changed it but that's had no effect either. As far as I can see the query looks OK - not sure why this is going wrong.

Edit: Also, this is using microsoft.sharepoint.client so .items() isn't available on the list object.


Solution

  • The CAML in your question has syntax problems because CAML is case sensitive.

    <where> should be <Where>, </where> should be </Where>, and </value> should be </Value>. (Similary, <eq> should be <Eq> and <contains> should be <Contains>.)

    You should wrap the entire CAML query in <View></View> tags when setting it as the value for the .ViewXml property. If you want to add a <RowLimit> element, it would come after the closing </Query> tag but before the closing </View> tag.