Freigeben über


Codeausschnitt: Ausführen einer Updatemethodeninstanz eines externen Inhaltstyps

Letzte Änderung: Donnerstag, 6. Mai 2010

Gilt für: SharePoint Server 2010

Inhalt dieses Artikels
Beschreibung
Voraussetzungen
So verwenden Sie dieses Beispiel

Beschreibung

Im folgenden Codebeispiel wird veranschaulicht, wie Sie eine Updater-Methodeninstanz mit einem externen Inhaltstyp unter Verwendung des BDC-Laufzeitobjektmodells auf dem Server programmgesteuert ausführen.

Voraussetzungen

  • Microsoft SharePoint Server 2010 oder Microsoft SharePoint Foundation 2010 auf dem Server

  • Microsoft .NET Framework 3.5 auf dem Clientcomputer

  • Microsoft Visual Studio.

  • Mindestens ein registrierter externer Inhaltstyp im BDC-Metadatenspeicher

So verwenden Sie dieses Beispiel

  1. Starten Sie Visual Studio, und erstellen Sie ein C#-Konsolenanwendungsprojekt. Stellen Sie sicher, dass Sie beim Erstellen des Projekts .NET Framework 3.5 auswählen.

  2. Klicken Sie im Menü Ansicht auf Eigenschaftenseiten, um die Projekteigenschaften aufzurufen.

  3. Wählen Sie auf der Registerkarte Build unter Zielplattform die Option Beliebige CPU aus.

  4. Schließen Sie das Fenster mit den Projekteigenschaften.

  5. Entfernen Sie im Projektmappen-Explorer unter Verweise sämtliche Projektverweise bis auf System und System.Core.

  6. Fügen Sie dem Projekt die folgenden Verweise hinzu:

    1. Microsoft.BusinessData

    2. Microsoft.SharePoint

    3. System.Web

  7. Ersetzen Sie den automatisch generierten Code in Program.cs durch den Code am Ende dieses Verfahrens.

  8. Ersetzen Sie die Werte von <siteUrl>, nameSpace> und <entityName> durch gültige Werte.

  9. Speichern Sie das Projekt.

  10. Kompilieren Sie das Projekt, und führen Sie es aus.

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

Siehe auch

Referenz

BdcService

Services

IMetadataCatalog

GetDatabaseBackedMetadataCatalog(SPServiceContext)

GetEntity(String, String)

IEntity

GetLobSystem()

GetLobSystemInstances()

ILobSystemInstance

Identity

IEntityInstance

FindSpecific(Identity, String, ILobSystemInstance)

GetFinderView(String)

IView

Fields

IFieldCollection

Update()