3

I have a list and want to return all items in a specific column. The code I have below isn't working, is the caml query wrong?

CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<Query><Where><Eq><FieldRefName='" + fieldInternalName + "'/></Eq></Where></Query>";
var itemColl = list.GetItems(camlQuery);
clientContext.Load(itemColl);
clientContext.ExecuteQuery();

var count = itemColl.Count; //returns 0, should not be

2 Answers 2

2

You have not specified the what the field should contain. For example, if the field name is country and you are searching for all countries named belgium, you need to put that in the query.

Replace the ViewXml as below. You need to add <View> and the condition as well:

camlQuery.ViewXml = "<View><Query><Where><Eq><FieldRefName='" + fieldInternalName + "'/><Eq><Value Type='Text'>Belgium</Value></Eq></Where></Query></View>";

Your code should be as below:

CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View><Query><Where><Eq><FieldRefName='" + fieldInternalName + "'/><Eq><Value Type='Text'>Belgium</Value></Eq></Where></Query></View>";    
var itemColl = list.GetItems(camlQuery);
clientContext.Load(itemColl);
clientContext.ExecuteQuery();

var count = itemColl.Count;

Updated as per comment:

You need to write below code:

CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View><RowLimit>100</RowLimit></View>";    

ListItemCollection listItems = oList.GetItems(camlQuery);
clientcontext.Load(listItems, items => items.Include(   
item => item["Country"]));  

clientContext.ExecuteQuery();

var count = listItems.Count;

In the load() method, you need to specify the Internal column name.

1
  • Actually I'm not trying to get a field by value, just want all items from a specific columns May 11, 2017 at 19:20
1

Assuming C# CSOM, you need a CAML AllItemsQuery, then a LINQ-based retrieval specification in the Load method call:

var itemColl = list.GetItems(CamlQuery.CreateAllItemsQuery());
clientContext.Load(itemColl, eachItem => eachItem.Include(
        item => item[fieldInternalName],
    ));
clientContext.ExecuteQuery();

See this awesome blog post by Karine Bosch: CAML and the Client Object Model

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.