Compartir a través de


Fragmento de código: ejecución de la instancia del método Creator 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

En el siguiente ejemplo de código se muestra cómo ejecutar mediante programación una instancia de método Creator de un tipo de contenido externo con el modelo de objetos de 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 nuevo proyecto de aplicación de consola C#. Seleccione .NET Framework 3.5 cuando cree 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 por 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.BusinessData.SharedService;
using Microsoft.BusinessData.MetadataModel;
using Microsoft.BusinessData.MetadataModel.Collections;
using Microsoft.BusinessData.Runtime;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;

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

        //How To: Create an External Content Type
        public static void BDCCreate()
        {
            //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;

                    IView createView = entity.GetCreatorView("Create");
                    IFieldValueDictionary fieldValueDictionary =
                        createView.GetDefaultValues();

                    // The following will work only for Sales.Customer table in AdventureWorks2008

                    //Enter number of Customers To Create
                    Console.Write("\nEnter number of Customers To Create : ");
                    int number = int.Parse(Console.ReadLine());

                    // Code below can be used to edit the values and create Records.
                    for (int i = 1; i <= number; i++)
                    {
                        // Example values for TerritoryID : 1,2,4
                        Console.Write("\nEnter the TerritoryID : ");
                        fieldValueDictionary["TerritoryID"] = int.Parse(Console.ReadLine());
                        // Example value CustomerType : S
                        Console.Write("\nEnter the CustomerType : ");
                        fieldValueDictionary["CustomerType"] = Console.ReadLine();
                        fieldValueDictionary["rowguid"] = Guid.NewGuid();
                        fieldValueDictionary["ModifiedDate"] = DateTime.Now;
                        Identity id = entity.Create(fieldValueDictionary, LobSysteminstance);
                        Console.WriteLine("Successfully Inserted Item Number: {0}", i);
                    }
                }
                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

IView

GetCreatorView(String)

GetDefaultValues()