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
Start Visual Studio on the client computer and create a C# Console application project. Select .NET Framework 3.5 when you create the project.
From the View menu, click Property Pages to bring up the project properties.
In the Build tab, for the Platform target, select Any CPU.
Close the project properties window.
In Solution Explorer, under References, remove all project references except for System and System.Core.
Add the following references to the project:
Microsoft.SharePoint.Client
Microsoft.SharePoint.Client.Runtime
System.XML
Replace the autogenerated code in Program.cs with the code listed at the end of this procedure.
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.
Save the project.
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();
}
}
}