I have a caml query to retrieve only specific row which matched to the criteria, but it returns all the items in the LIST,

@"<Where><BeginsWith><FieldRef Name='custaname' /><Value Type='Lookup'>Prab</Value></BeginsWith></Where>";

with c# code it is like below

string getAllquery = @"<Where><BeginsWith><FieldRef Name='custaname' /><Value Type='Lookup'>Prab</Value></BeginsWith></Where>";
            ServerProperties.custom.ServerProperties SP = new ServerProperties.custom.ServerProperties();
            SPWeb _SPWeb = SP.GetConnected();
            SPList _SPlist = _SPWeb.Lists["Orders"];
            SPListItemCollection PubCollection = _SPlist.GetItems(getAllquery);

This query working perfect with CAML builder.

what i have missed here.?

  • Do you know that you need to use item ID when using Lookup in a CAML query? Apr 23, 2015 at 9:49
  • then how is it working in CAML builder?
    – mzonerz
    Apr 23, 2015 at 10:07

2 Answers 2


Adding Orderby element was helped to make it work as follows.

 @"<Where><BeginsWith><FieldRef Name='custaname' /><Value Type='Lookup'>Prab</Value></BeginsWith></Where><orderby><FieldRef Name='title' Ascending='false;/></orderby>";

Your code

"<Where><BeginsWith><FieldRef Name='custaname' /><Value Type='Lookup'>Prab</Value></BeginsWith></Where>"

Try add <View><Query>...</Query></View> in your code

"<View><Query><Where><BeginsWith><FieldRef Name='custaname' /><Value Type='Lookup'>Prab</Value></BeginsWith></Where></Query></View>"

Look this https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.list.getitems.aspx

Something like this

CamlQuery query = new CamlQuery();
        query.ViewXml = "<View><Query><Where><BeginsWith><FieldRef Name='custaname' /><Value Type='Lookup'>Prab</Value></BeginsWith></Where></Query></View>";
        ListItemCollection collListItem = targetList.GetItems(query);


For query.ViewXml write

CamlQuery query = new CamlQuery();
        query.ViewXml = "<View><Query><Where>...</Where></Query></View>";

And for query.Query write without <View> and <Query>

CamlQuery query = new CamlQuery();
        query.Query = "<Where>...</Where>";
  • tthank you for your answers,does it work when it comes to c# with "query" tag?
    – mzonerz
    Apr 23, 2015 at 10:16
  • .Query already have <Query> tag
    – Marko Tica
    Apr 23, 2015 at 11:31

Your Answer

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

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