
18
❘
CHAPTER 1 Leveraging the SharePoint object ModeL
You can also update properties of the list with the Client OM API. To do this you need to load
the list using the client context. You can do this with a LINQ to objects query or CAML query,
or by calling the
GetByTitle method on the clientContext.Webs.Lists object and passing the
title of the list. The
GetByTitle approach has the benefit of loading only the list that you modify
instead of loading all the lists in your query. After you load the list, you can set the property that
you want to update, and call the
Update method on the list object. Of course, you also must call
ExecuteQuery on the client context to send the update to SharePoint, as follows:
List oList = clientContext.Web.Lists.GetByTitle(“Custom Announcements”);
oList.Description = “This is the new Custom Announcements List”;
oList.Update();
clientContext.ExecuteQuery();
Another common list management task is adding a eld to an existing list. To perform this task, you
need to get a reference to the list object that you want to add the eld to. You can add a eld with
any of the types available from the user SharePoint 2010 interface. You can then use the
AddField
or
AddFieldAsXml method to add a field. If you need to add attributes to your new field, you can
also do that. The following code creates a new number field and sets the minimum and maximum
values for t