Code Snippet: Update an Item in an External List on the Client
Applies to: SharePoint Server 2010
In this article
Description
Prerequisites
To use this example
Description
You use the Update method of the List class to update an item in an external list from the client. The following code snippet shows you how to update an item in an external list using the client object model.
Prerequisites
Microsoft SharePoint Server 2010 or Microsoft SharePoint Foundation 2010 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
Replace the autogenerated code in Program.cs with the code listed at the end of this procedure.
Replace the values of <TargetSiteUrl> , <TargetListName>, MyField/MyValue pairs, 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;
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 show 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 specificItem = externalList.GetItemById(
"<BdcIdentity>");
specificItem["MyField"] = "MyValue";
// Set all required fields
specificItem.Update();
clientContext.ExecuteQuery();
}
}
}