Creating a list item
To create list items, you create a ListItemCreationInformation object, set its properties, and pass it as parameter to the AddItem(ListItemCreationInformation) method of the List class. Set properties on the list item object that this method returns, and then call the Update() method, as seen in the following example.
using System;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;
namespace Microsoft.SDK.SharePointServices.Samples { class CreateListItem { static void Main() { string siteUrl = "http://MyServer/sites/MySiteCollection"; ClientContext clientContext = new ClientContext(siteUrl); SP.List oList = clientContext.Web.Lists.GetByTitle("Announcements"); ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation(); ListItem oListItem = oList.AddItem(itemCreateInfo); oListItem["Title"] = "My New Item!"; oListItem["Body"] = "Hello World!"; oListItem.Update(); clientContext.ExecuteQuery(); } } }
Because the previous example creates a standard list item, you do not need to set properties on the ListItemCreationInformation object before it is passed to the AddItem(ListItemCreationInformation) method. However, if your code must create a new folder, for example, you must set the UnderlyingObjectType of the ListItemCreationInformation to Folder.
For information and an example about how to create a list item object within the context of the Microsoft SharePoint Foundation 2010 Silverlight object model, see Using the Silverlight Object Model.
Updating a list item
To set most list item properties, you can use a column indexer to make an assignment, and call the Update() method so that changes will take effect when you call ExecuteQuery() or ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler). The following example sets the title of the third item in the Announcements list.
using System;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;
namespace Microsoft.SDK.SharePointServices.Samples { class UpdateListItem { static void Main() { string siteUrl = "http://MyServer/sites/MySiteCollection"; ClientContext clientContext = new ClientContext(siteUrl); SP.List oList = clientContext.Web.Lists.GetByTitle("Announcements"); ListItem oListItem = oList.Items.GetById(3); oListItem["Title"] = "My Updated Title."; FieldUrlValue url = new FieldUrlValue();
url.Description = "desc";
url.Url = "http://path";
listItem["URL"] = url;
oListItem.Update(); clientContext.ExecuteQuery(); } } }
Deleting a list item
To delete a list item, call the DeleteObject() method on the object. The following example uses the GetItemById() method to return the second item from the list, and then deletes the item.
SharePoint Foundation 2010 maintains the integer IDs of items within collections, even if they have been deleted. So, for example, the second item in a list might not have 2 as its identifier. A ServerException is returned if the DeleteObject() method is called for an item that does not exist.
using System;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;
namespace Microsoft.SDK.SharePointServices.Samples { class DeleteListItem { static void Main() { string siteUrl = "http://MyServer/sites/MySiteCollection"; ClientContext clientContext = new ClientContext(siteUrl); SP.List oList = clientContext.Web.Lists.GetByTitle("Announcements"); ListItem oListItem = oList.GetItemById(2); oListItem.DeleteObject(); clientContext.ExecuteQuery(); } } }
If you want to retrieve, for example, the new item count that results from a delete operation, include a call to the Update() method to refresh the list. In addition, you must load either the list object itself or the ItemCount property on the list object before executing the query. If you want to retrieve both a start and end count of the list items, you must execute two queries and return the item count twice, as shown in the following modification of the previous example.
using System;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;
namespace Microsoft.SDK.SharePointServices.Samples { class DeleteListItemDisplayCount { static void Main() { string siteUrl = "http://MyServer/sites/MySiteCollection"; ClientContext clientContext = new ClientContext(siteUrl); SP.List oList = clientContext.Web.Lists.GetByTitle("Announcements"); clientContext.Load(oList, list => list.ItemCount); clientContext.ExecuteQuery(); int startCount = oList.ItemCount; ListItem oListItem = oList.GetItemById(2); oListItem.DeleteObject(); oList.Update(); clientContext.Load(oList, list => list.ItemCount); clientContext.ExecuteQuery(); int endCount = oList.ItemCount; Console.WriteLine("Start: {0} End: {1}", startCount, endCount); } } }
PowerShell
Add-Type -Path "C:\Z_Disk\Global TFS 2019\KLCN\CodeLibrary\CNKLSHSV2030\KLCN.NinTex.Webservices\KLCN.NinTex.Webservices\Library\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Z_Disk\Global TFS 2019\KLCN\CodeLibrary\CNKLSHSV2030\KLCN.NinTex.Webservices\KLCN.NinTex.Webservices\Library\Microsoft.SharePoint.Client.dll"
$siteUrl = "http://cnklshsv0003/fcschina/";
$context = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$web = $context.Web
$list = $web.Lists.GetByTitle("Javi_List")
$item = $list.GetItemById(47)
$urlValue = New-Object Microsoft.SharePoint.Client.FieldUrlValue
$urlValue.Url = "http://www.baidu.com"
$urlValue.Description = "Description of the URL"
$item["url"] = [Microsoft.SharePoint.Client.FieldUrlValue]$urlValue
$item.Update()
$context.ExecuteQuery()