Getting the values โ€‹โ€‹of a list box field from an SP using a client object model

What is the best way to get list items and their properties from an SP list using a 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 the list of elements in the context, and, if possible, load the values โ€‹โ€‹of the element fields into the first execution request.

I tried using the following

  context.Load(items, itema => itema.Include( item => item, item=> item.FieldValues, item => item["CustomerId"])); 

which does not work.

Can anyone provide a cleaner solution?

+7
source share
3 answers

The most efficient way to query SharePoint is to use a CAML request. Call (SP) List.GetItems (camlQuery). You will always get an instance (SP) of ListItemCollection.

Thus, the most effective query will look like this:

 string server = "http://localhost"; var ctx = new ClientContext(server); var web = ctx.Web; var list = web.Lists.GetByTitle("Contacts"); var listItemCollection = list.GetItems(CamlQuery.CreateAllItemsQuery()); // always use QueryTrimming to minimize size of // data that has to be transfered ctx.Load(listItemCollection, eachItem => eachItem.Include( item => item, item => item["CustomerId"])); // ExecuteQuery will pull all data from SharePoint // which has been staged to Load() ctx.ExecuteQuery(); foreach(ListItem listItem in listItemCollection) { Console.WriteLine(listItem["CustomerId"]); } 

Thorsten

+5
source

I am not 100% sure what properties you would like to get from the fields, but you could play with the following:

 SPSite oSite = new SPSite("http://localhost"); SPWeb oWeb = oSite.OpenWeb(); SPList oList = oWeb.Lists["LIST NAME"]; SPFieldCollection oFields = oList.Fields; foreach (SPField oField in oFields) { Console.WriteLine("Property: " + oField.GetProperty("PROPERTY")); } 

OR

The property you are looking for may actually be under the SPField object. Look at the available properties and methods here: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfield_members(v=office.12).aspx

Hope this helps. If there is not any additional information about what actual properties you want to get from the list boxes, maybe you will get a more accurate solution?

0
source

Change your code.

 IQueryable<ListItem> items = spList.GetItems(query); 

Then call LoadQuery () instead of Load ()

 context.LoadQuery(items); 

You can add additional expressions to read the ListItem property you like:

 context.LoadQuery(items.Include(item => item["CustomerId"])); 

I have a problem trying to do item => item.FieldValues , but item => item.FieldValuesAsText works. I'm not sure why :( But just doing context.LoadQuery(items) should do what you want.

0
source

All Articles