Creating, reading, updating, and deleting
In the conditional scope shown in Example 4, a new list is created if the user has the appropriate permissions. Creating new lists and items using the managed client object model is done with the creation information objects. Using the ListCreationInformation and ListItemCreationInformation
objects, you can define all of the necessary values for a list or item
and then send that data with the batch back to the server. Example 5 shows how to use these objects to create a new list and list item.
Example 5. Creating a list and list item
string appWebUrl = Page.Request["SPAppWebUrl"];
using (ClientContext ctx = new ClientContext(appWebUrl))
{
//Create a new list
ListCreationInformation listCI = new ListCreationInformation();
listCI.Title = "My List";
listCI.Description += "A list for use with the Client OM";
listCI.TemplateType = (int)ListTemplateType.GenericList;
listCI.QuickLaunchOption = Microsoft.SharePoint.Client.QuickLaunchOptions.On;
List list = ctx.Web.Lists.Add(listCI);
ctx.ExecuteQuery();
//Create a new list item
ListItemCreationInformation listItemCI = new ListItemCreationInformation();
ListItem item = list.AddItem(listItemCI);
item["Title"] = "New Item";
item.Update();
ctx.ExecuteQuery();
}
If you would like to return items from a list by using CSOM, you must write Collaborative Application Markup Language (CAML) queries. CAML queries are created for the managed client object model via the CamlQuery object. This object has a ViewXml property that accepts a CAML query designating the items to return. Example 6 demonstrates running a CAML query against a list.
Example 6. Using CAML to return list items
string appWebUrl = Page.Request["SPAppWebUrl"];
using (ClientContext ctx = new ClientContext(appWebUrl))
{
//Read the Site, List, and Items
ctx.Load(ctx.Web);
List myList = ctx.Web.Lists.GetByTitle("My List");
ctx.Load(myList);
StringBuilder caml = new StringBuilder();
caml.Append("<View><Query>");
caml.Append("<Where><Eq><FieldRef Name='Title'/>");
caml.Append("<Value Type='Text'>New Item</Value></Eq></Where>");
caml.Append("</Query><RowLimit>100</RowLimit></View>");
CamlQuery query = new CamlQuery();
query.ViewXml = caml.ToString();
ListItemCollection myItems = myList.GetItems(query);
ctx.Load(myItems);
ctx.ExecuteQuery();
Response.Write("<p>Site: " + ctx.Web.Title + "</p>");
Response.Write("<p>List: " + myList.Title + "</p>");
Response.Write("<p>Item Count: " + myItems.Count.ToString() + "</p>");
}
Updating through
the managed client object model is straightforward. In most cases, you
will simply set the value of a property and then call the appropriate Update method. Example 7 presents samples of updating a site, list, and list item.
Example 7. Update operations
//Update the Site, List, and Items
ctx.Web.Description = "Client OM samples";
ctx.Web.Update();
myList.Description = "Client OM data";
myList.Update();
foreach (ListItem myItem in myItems)
{
myItem["Title"] = "Updated";
myItem.Update();
}
ctx.ExecuteQuery();
Response.Write("<p>Site: " + ctx.Web.Description + "</p>");
Response.Write("<p>List: " + myList.Description + "</p>");
Response.Write("<p>Item Count: " + myItems.Count.ToString()+ "</p>");
Deleting objects with the managed client object model involves calling the DeleteObject
method. This method is the same across most objects that can be
deleted. The following code shows how to delete the list created
earlier:
myList.DeleteObject();
ctx.ExecuteQuery();
Along with lists, you’ll also want to work with libraries. Document
libraries are handled in the managed client object model similarly to
lists. Of course, the major difference is handling documents.
Fortunately, uploading documents to libraries by using the managed
client object model is very similar to using the server object model;
you must upload the document using the URL of the folder in which you
want to store the document. Example 8 shows a full set of create, read, update, and delete operations around a file and a document library.
Example 8. Working with document libraries
string appWebUrl = Page.Request["SPAppWebUrl"];
using (ClientContext ctx = new ClientContext(appWebUrl))
{
//Get site
Web site = ctx.Web;
ctx.Load(site);
ctx.ExecuteQuery();
//Create a new library
ListCreationInformation listCI = new ListCreationInformation();
listCI.Title = "My Docs";
listCI.Description = "A library for use with Client OM";
listCI.TemplateType = (int)ListTemplateType.DocumentLibrary;
listCI.QuickLaunchOption = Microsoft.SharePoint.Client.QuickLaunchOptions.On;
List list =site.Lists.Add(listCI);
ctx.ExecuteQuery();
//Create a document
MemoryStream m = new MemoryStream();
StreamWriter w = new StreamWriter(m);
w.Write("Some content for the document.");
w.Flush();
//Add it to the library
FileCreationInformation fileCI = new FileCreationInformation();
fileCI.Content = m.ToArray();
fileCI.Overwrite = true;
fileCI.Url = appWebAppUrl + "/My%20Docs/MyFile.txt";
Folder rootFolder = site.GetFolderByServerRelativeUrl("My%20Docs");
ctx.Load(rootFolder);
Microsoft.SharePoint.Client.File newFile = rootFolder.Files.Add(fileCI);
ctx.ExecuteQuery();
//Edit Properties
ListItem newItem = newFile.ListItemAllFields;
ctx.Load(newItem);
newItem["Title"] = "My new file";
newItem.Update();
ctx.ExecuteQuery();
//Delete file
newItem.DeleteObject();
ctx.ExecuteQuery();
}