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? Commented 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. Commented Aug 6, 2014 at 6:46
  • you tested with ID or with string with the tool? please try RowLimit Commented Aug 6, 2014 at 6:47
  • With the title. Commented 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? Commented 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/… Commented Aug 6, 2014 at 6:54
  • 2
    Another useful bookmark. Thank you a lot. I need to wait 3 minutes to accept your answer. Commented 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? ;) Commented Aug 6, 2014 at 6:56
  • I didn't see your answer...so post it :) Commented Aug 6, 2014 at 7:01
  • I understand, but you do see it now I guess? :) Commented Aug 6, 2014 at 7:02

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.