Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Server

Sharepoint 2013 : Working with the CSOM (part 3) - Working with the managed client object model - Creating, reading, updating, and deleting

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
11/5/2014 8:32:48 PM

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();
}
Other -----------------
- Managing Windows Server 2012 Systems : Configuring Roles, Role Services, and Features (part 6) - Tracking installed roles, role services, and features
- Managing Windows Server 2012 Systems : Configuring Roles, Role Services, and Features (part 5) - Installing components at the prompt
- Managing Windows Server 2012 Systems : Configuring Roles, Role Services, and Features (part 4) - Managing server binaries
- Managing Windows Server 2012 Systems : Configuring Roles, Role Services, and Features (part 3) - Adding server roles and features
- Managing Windows Server 2012 Systems : Configuring Roles, Role Services, and Features (part 2) - Installing components with Server Manager - Viewing configured roles and role services
- Managing Windows Server 2012 Systems : Configuring Roles, Role Services, and Features (part 1) - Using roles, role services, and features
- Windows Server 2012 : Configuring IPsec (part 7) - Configuring connection security rules - Monitoring IPsec
- Windows Server 2012 : Configuring IPsec (part 6) - Configuring connection security rules - Creating a custom rule, Configuring authenticated bypass
- Windows Server 2012 : Configuring IPsec (part 5) - Configuring connection security rules - Creating an authentication exemption rule, Creating a server-to-server rule, Creating a tunnel rule
- Windows Server 2012 : Configuring IPsec (part 4) - Configuring connection security rules - Types of connection security rules, Creating an isolation rule
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server