Export (0) Print
Expand All

How to: Create, Update, and Delete List Items

SharePoint 2010

Last modified: April 29, 2011

Applies to: SharePoint Foundation 2010

In this article
Creating a list item
Updating a list item
Deleting a list item

Available in SharePoint Online

Creating, updating, or deleting list items through the client object model works similarly to performing these tasks through the server object model. You create a list item object, set its properties, and then update the object. To modify or delete a list item object, use the GetById() method of the ListItemCollection class to return the object, and then either set properties and call update on the object that this method returns, or call the object's own method for deletion. Unlike the server object model, each of these operations in the client object model must conclude with a call to ExecuteQuery() or ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler) (JavaScript: executeQueryAsync(succeededCallback, failedCallback)) for changes to take effect on the server.

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.

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.";

            oListItem.Update();

            clientContext.ExecuteQuery(); 
        }
    }
}

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);
        }
    }
}
Show:
© 2014 Microsoft