5

I am querying a list to return only items matching a certain title, but the query returns all items. It seems I built or execute the query the wrong way:

/*
SP is an alias for Microsoft.SharePoint.Client
SP = using Microsoft.SharePoint.Client
*/
var list = clientContext.Web.Lists.GetByTitle(listTitle);

 SP.CamlQuery query = new SP.CamlQuery();
 string qs = String.Format("<Query><Where><Eq><FieldRef Name=\"Title\"></FieldRef><Value Type=\"Text\">{0}</Value></Eq></Where></Query>", listItemTitle);
 query.ViewXml = qs;

 SP.ListItemCollection oCol = list.GetItems(query);
 clientContext.Load(oCol);
 clientContext.ExecuteQuery();
 SP.ListItem item = oCol[0];

 clientContext.Load(item, i => i.File);
 clientContext.ExecuteQuery();

Based on the query oCol should only contain 1 Item, since the titles are definitly unique, but it returns 58 results (all items) and therefor the item at oCol[0] is not the correct one.

Please note: I was getting the Item and file by Id before, but the client wants a way to enter an error-prone string, so I need to use CAML.

How can I correct this?

4
  • did you tried this query in U2U caml query builder just to check if it's returning one item or more? Aug 6, 2014 at 6:46
  • Not U2U, but SP CAML Query Helper and the query returns just 1 Item. the query itself is correct.
    – Marco
    Aug 6, 2014 at 6:46
  • you tested with ID or with string with the tool? please try RowLimit Aug 6, 2014 at 6:47
  • With the title.
    – Marco
    Aug 6, 2014 at 6:49

2 Answers 2

11

You are missing the <View> tags in your ViewXml (see example here)

It should be

 string qs = String.Format("<View><Query><Where><Eq><FieldRef Name=\"Title\"></FieldRef><Value Type=\"Text\">{0}</Value></Eq></Where></Query></View>", listItemTitle);
 query.ViewXml = qs;

In case of an invalid ViewXml, SharePoint returns all items (in the client API). In the client API, as opposed to server-side API, you create the full view instead of just setting the Query property

3
  • Robert this works. Would you please care to elaborate why I need to use the <View> tags in code, but not in the Query Builder?
    – Marco
    Aug 6, 2014 at 6:53
  • It is because you are not just setting the Query property as you would server side, you are actually setting the whole View, which can include RowLimit, ViewFields etc besides the actual Query: msdn.microsoft.com/en-us/library/… Aug 6, 2014 at 6:54
  • 2
    Another useful bookmark. Thank you a lot. I need to wait 3 minutes to accept your answer.
    – Marco
    Aug 6, 2014 at 6:55
2

Change your query like :

 CamlQuery query = new SP.CamlQuery();
    query.ViewXml = "@<View><Query><Where><Eq><FieldRef Name='Title' /><ValueType='Text'>{Title Text Here}</Value></Eq></Where></Query></View>"; 
    var itemts= list.GetItems(query);
3
  • So, what I said 5 minutes ago? ;) Aug 6, 2014 at 6:56
  • I didn't see your answer...so post it :) Aug 6, 2014 at 7:01
  • I understand, but you do see it now I guess? :) Aug 6, 2014 at 7:02

Your Answer

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

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