Basic Operation’s That SharePointer Needs For SharePointing By Using CSOM – Part 3

Previously in part one we discussed the main operation that developers needs to deal with SharePoint site , and in part 2 , we talked about  SharePoint list , and the bellow examples show how to use the .NET Framework CSOM to complete list item -related tasks.

SharePoint list item tasks

These examples demonstrate how to use the .NET Framework CSOM (Client side object model) to complete tasks that are related to list items.

Retrieve items from a SharePoint list

This example retrieves the items in a SharePoint list. You also need to add a using statement for Microsoft.SharePoint.Client.QueryExpression.

Note

You can use the FolderServerRelativeUrl property to further restrict the items that are returned to those in a specified folder.

C#
// Starting with ClientContext, the constructor requires a URL to the 
// server running SharePoint. 
ClientContext context = new ClientContext("http://developersportal.net"); 

// Assume the web has a list named "Announcements". 
List announcementsList = context.Web.Lists.GetByTitle("Announcements"); 

// This creates a CamlQuery that has a RowLimit of 100, and also specifies Scope="RecursiveAll" 
// so that it grabs all list items, regardless of the folder they are in. 
CamlQuery query = CamlQuery.CreateAllItemsQuery(100); 
ListItemCollection items = announcementsList.GetItems(query); 

// Retrieve all items in the ListItemCollection from List.GetItems(Query). 
context.Load(items); 
context.ExecuteQuery(); 
foreach (ListItem listItem in items) 
{ 
    // We have all the list item data. For example, Title. 
    label1.Text = label1.Text + ", " + listItem["Title"]; 
} 

Create a new list item

This example creates a new SharePoint list item by using the ListItemCreationInformation class.

C#
// Starting with ClientContext, the constructor requires a URL to the 
// server running SharePoint. 
ClientContext context = new ClientContext("http://developersportal.net"); 

// Assume that the web has a list named "Announcements". 
List announcementsList = context.Web.Lists.GetByTitle("Announcements"); 

// We are just creating a regular list item, so we don't need to 
// set any properties. If we wanted to create a new folder, for 
// example, we would have to set properties such as 
// UnderlyingObjectType to FileSystemObjectType.Folder. 
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation(); 
ListItem newItem = announcementsList.AddItem(itemCreateInfo); 
newItem["Title"] = "New Item!"; 
newItem["Body"] = "Hello Developers world!"; 
newItem.Update(); 

context.ExecuteQuery();  

Update a list item

This example updates a SharePoint list item.

C#
// Starting with ClientContext, the constructor requires a URL to the 
// server running SharePoint. 
ClientContext context = new ClientContext("http://developersportal.net"); 

// Assume that the web has a list named "Announcements". 
List announcementsList = context.Web.Lists.GetByTitle("Announcements"); 

// Assume there is a list item with ID=1. 
ListItem listItem = announcementsList.GetItemById(1); 

// Write a new value to the Body field of the Announcement item.
listItem["Body"] = "This is my new value!!"; 
listItem.Update(); 

context.ExecuteQuery();  

Delete a list item

This example deletes a SharePoint list item.

C#
// Starting with ClientContext, the constructor requires a URL to the 
// server running SharePoint. 
ClientContext context = new ClientContext("http://developersportal.net"); 

// Assume that the web has a list named "Announcements". 
List announcementsList = context.Web.Lists.GetByTitle("Announcements"); 

// Assume that there is a list item with ID=2. 
ListItem listItem = announcementsList.GetItemById(2); 
listItem.DeleteObject(); 

context.ExecuteQuery(); } 
Please follow and like us:

3 thoughts on “Basic Operation’s That SharePointer Needs For SharePointing By Using CSOM – Part 3

  1. I’m not sure where you are getting your info, but good topic.
    I needs to spend some time learning more or understanding more.
    Thanks for great information I was looking for this information for my mission.

  2. Have you ever considered about including a little bit more than just your articles? I mean, what you say is fundamental and all. Nevertheless imagine if you added some great pictures or videos to give your posts more, “pop”! Your content is excellent but with images and clips, this site could certainly be one of the best in its niche. Terrific blog!

Leave a Reply

Your email address will not be published. Required fields are marked *