What is the optimal way to get a list items and their properties from a SP list using Client object model?
Here is the code I'm using.
string server = "http://localhost";
ClientContext context = new ClientContext(server);
Web web = context.Web;
var spList = web.Lists.GetByTitle("Contact");
CamlQuery query = new CamlQuery();
var items = spList.GetItems(query);
context.Load(items,
itema => itema.Include(
item => item,
item => item["CustomerId"]));
context.ExecuteQuery();
Console.WriteLine("Items");
foreach (var item in items.ToList())
{
context.Load(item);
}
context.ExecuteQuery();
foreach (var item in items)
{
foreach (var a in item.FieldValues)
{
Console.WriteLine(a.Key + ":" + a.Value.ToString());
}
}
I want to remove the single liner foreach used to load list item in the context and if possible load the item field values in the first Execute Query itself.
I tried using the following
context.Load(items,
itema => itema.Include(
item => item,
item=> item.FieldValues,
item => item["CustomerId"]));
which doesn't work.
Any one can provide a cleaner solution?