Code Snippet: Delete an Item From an External List on the Client

Applies to: SharePoint Server 2010

In this article
Description
Prerequisites
To use this example

Description

You use the DeleteObject method of the List class to delete an item from an external list on the client. The following code snippet shows you how to delete an item from an external list by using the client object model.

Prerequisites

  • Microsoft SharePoint Server 2010 or Microsoft SharePoint Foundation 2010 installed on the server.

  • At least one external list on the server.

  • Microsoft Office Professional Plus 2010 and Microsoft .NET Framework 3.5 on the client computer.

  • Microsoft Visual Studio.

To use this example

  1. Start Visual Studio on the client computer and create a C# Console application project. Select .NET Framework 3.5 when you create the project.

  2. From the View menu, click Property Pages to bring up the project properties.

  3. In the Build tab, for the Platform target, select Any CPU.

  4. Close the project properties window.

  5. In Solution Explorer, under References, remove all project references except for System and System.Core.

  6. Add the following references to the project:

    1. Microsoft.SharePoint.Client

    2. Microsoft.SharePoint.Client.Runtime

    3. System.XML

  7. Replace the autogenerated code in Program.cs with the code listed at the end of this procedure.

  8. Replace the values of <TargetSiteUrl> , <TargetListName>, and <BdcIdentity> with valid values. To learn how to get a valid BdcIdentity value, see Code Snippet: Get the BdcIdentity of All Items in an External List on the Server.

  9. Save the project.

  10. Compile and run the project.

using System;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Runtime;

namespace Microsoft.SDK.Sharepoint.Samples
{
    class Program
    {
        // Note: Replace these with your actual Site url and List name
        private static string TargetSiteUrl = "<TargetSiteUrl>";
        private static string TargetListName = "<TargetListName>";

        /// <summary>
        /// Example to illustrate using CSOM to retrieve external List data        
        /// </summary>        
        static void Main(string[] args)
        {
            ClientContext clientContext = new ClientContext(TargetSiteUrl);
            List externalList = 
                clientContext.Web.Lists.GetByTitle(TargetListName);
            ListItem specifcItem = externalList.GetItemById(
                "<BdcIdentity>");           
            specifcItem.DeleteObject();
            clientContext.ExecuteQuery();          
        }        
    }
}