1

we have to work on the client side of SharePoint, so I use Javascript, not C#. I want to do a query with "contains" like so

function onRequestSucceeded() {
    myList = website.get_lists().getByTitle("Prüfberichte");
    var query = '<Where><Contains><FieldRef Name="Name_x0020_Lieferant_x0020__x0028_Supplier_x0020_name_x0029_"/><Value Type="Text">Hans</Value></Contains></Where>';
    camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml(query);
    items = myList.getItems(camlQuery);
    clientContext.load(items, 'Include(Id, DisplayName, HasUniqueRoleAssignments)');
    clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);
}

Unfortunately, this returns the whole list, instead of the single item that contains "Hans".

This is a "contains"-specific problem, "Eq" works fine.

All help will be gladly appreciated!

Annika
  • 19
  • 2
  • I'm aware of that questions http://stackoverflow.com/questions/1870314/sharepoint-list-caml-query-using-contains didn't solve my problem, though – Annika Oct 26 '16 at 13:02
  • 1
    Does it work if you include the outer `` and `` tags around the query, as in [this example](http://stackoverflow.com/documentation/sharepoint/1316/working-with-javascript-client-object-model-jsom/7817/get-list-items-by-caml-query#t=201610261451135890854)? – Thriggle Oct 26 '16 at 14:52

1 Answers1

0

In SharePoint JSOM API SP.CamlQuery.viewXml property accepts XML schema which has the following structure:

<View>
    <Query>
          <Where>
               ...
          <Where> 
   </Query>
</View> 

for example:

var query=new SP.CamlQuery();
query.set_viewXml('<View Scope="RecursiveAll"><Query><Where><Eq><FieldRef Name="FSObjType" /><Value Type="Integer">1</Value></Eq></Where></Query></View>');

When invalid XML schema is provided (like in your case) the query is getting ignored and all the items are returned (standard behavior), most likely it is the reason why it occurs in your case.

Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193