Compartir a través de


Fragmento de código: Ejecutar una instancia del método actualizador de un tipo de contenido externo

Última modificación: jueves, 06 de mayo de 2010

Hace referencia a: SharePoint Server 2010

En este artículo
Descripción
Requisitos previos
Para usar este ejemplo

Descripción

El siguiente ejemplo de código muestra cómo ejecutar mediante programación una instancia del método Updater de un tipo de contenido externo mediante el modelo de objetos en tiempo de ejecución de BDC en el servidor.

Requisitos previos

  • Microsoft SharePoint Server 2010 o Microsoft SharePoint Foundation 2010 en el servidor.

  • Microsoft .NET Framework 3.5 en el equipo cliente.

  • Microsoft Visual Studio.

  • Al menos un tipo de contenido externo registrado en el repositorio de metadatos de BDC.

Para usar este ejemplo

  1. Inicie Visual Studio y cree un proyecto de aplicación de consola C#. Asegúrese de que selecciona .NET Framework 3.5 al crear el proyecto.

  2. En el menú Ver, haga clic en Páginas de propiedades para que aparezcan las propiedades del proyecto.

  3. En la ficha Compilación, para el Destino de la plataforma, seleccione Cualquier CPU.

  4. Cierre la ventana de propiedades del proyecto.

  5. En el Explorador de soluciones, en Referencias, quite todas las referencias del proyecto excepto System y System.Core.

  6. Agregue las siguientes referencias al proyecto:

    1. Microsoft.BusinessData

    2. Microsoft.SharePoint

    3. System.Web

  7. Reemplace el código generado automáticamente en Program.cs con el código que aparece al final de este procedimiento.

  8. Reemplace los valores de <siteUrl>, nameSpace> y <entityName> por valores válidos.

  9. Guarde el proyecto.

  10. Compile y ejecute el proyecto.

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.BusinessData.SharedService;
using Microsoft.BusinessData.MetadataModel;
using Microsoft.BusinessData.MetadataModel.Collections;
using Microsoft.BusinessData.Runtime;
using Microsoft.SharePoint.Administration;

namespace SDKSamples
{
    class Methods
    {
        static void Main(string[] args)
        {
            BDCUpdate();
        }

        //How To: Edit an item from an External Content Type
        public static void BDCUpdate()
        {
            //Specify the SiteURL, Namespace and the Entity Name
            string SiteURL = "<siteUrl>";
            string nameSpace = "<nameSpace>";
            string entityName = "<entityName>";
           
            using (SPSite site = new SPSite(SiteURL))
            {
                using (new Microsoft.SharePoint.SPServiceContextScope(
                    SPServiceContext.GetContext(site)))
                {
                    BdcService service =
                        SPFarm.Local.Services.GetValue<BdcService>(
                        String.Empty);
                    IMetadataCatalog catalog =
                        service.GetDatabaseBackedMetadataCatalog(
                        SPServiceContext.Current);

                    IEntity entity = catalog.GetEntity(
                        nameSpace, entityName);
                    ILobSystemInstance LobSysteminstance =
                        entity.GetLobSystem().
                        GetLobSystemInstances()[0].Value;

                    // Accept the user input for identity value
                    Console.Write(
                        "\nEnter identity value for which you want to edit : ");
                    int identityColumnValue =
                        int.Parse(Console.ReadLine());
                    Identity identity =
                        new Identity(identityColumnValue);

                    try
                    {
                        IEntityInstance ientityinstance =
                            entity.FindSpecific(
                            identity, "Read Item", LobSysteminstance);
                        IFieldCollection fieldCollection =
                            entity.GetFinderView("Read List").Fields;

                        //Display the old values
                        Console.WriteLine("\nOld Values : ");

                        foreach (IField field in fieldCollection)
                        {
                            Console.WriteLine(
                                field.Name.PadRight(20) + ":" +
                                ientityinstance[field.Name].ToString());
                        }

                        // The following will work only for Sales.Customer table in AdventureWorks2008
                        // Changing value of "TerritoryID" field
                        string fieldName = "TerritoryID";
                        
                        // Example Value for TerritoryID will be 1,4,6 etc.
                        Console.Write(
                           "\nEnter the new value for the column {0}: ",
                           fieldName);
                        ientityinstance[fieldName] = int.Parse(Console.ReadLine());
                        ientityinstance["ModifiedDate"] = DateTime.Now;

                        ientityinstance.Update();
                        Console.WriteLine("Record updated");
                        
                        //Display the Updated values
                        Console.WriteLine("\nUpdated Values : ");

                        foreach (IField field in fieldCollection)
                        {
                            Console.WriteLine(
                                field.Name.PadRight(20) + ":" +
                                ientityinstance[field.Name].ToString());
                        }

                    }
                    catch (ObjectNotFoundException exception)
                    {
                        Console.WriteLine(
                            "Identity column with value {0} not found...",
                            identityColumnValue);
                    }

                }
            }
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
    }
}

Vea también

Referencia

BdcService

Services

IMetadataCatalog

GetDatabaseBackedMetadataCatalog(SPServiceContext)

GetEntity(String, String)

IEntity

GetLobSystem()

GetLobSystemInstances()

ILobSystemInstance

Identity

IEntityInstance

FindSpecific(Identity, String, ILobSystemInstance)

GetFinderView(String)

IView

Fields

IFieldCollection

Update()