Condividi tramite


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

  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

  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>, 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.

  9. Save the project.

  10. 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();          
        }        
    }
}