Freigeben über


Codeausschnitt: Importieren eines BDC-Modells in den Metadatenspeicher

Letzte Änderung: Donnerstag, 13. Mai 2010

Gilt für: SharePoint Server 2010

Im folgenden Beispiel wird gezeigt, wie Sie ein BDC-Modell in einen Farmkatalog importieren.

Voraussetzungen:

  • Microsoft SharePoint Server 2010 oder Microsoft SharePoint Foundation 2010.

  • Microsoft .NET Framework 3.5.

So verwenden Sie dieses Beispiel

  1. Starten Sie Visual Studio, und erstellen Sie ein C#-Konsolenanwendungsprojekt. Wählen Sie beim Erstellen des Projekts .NET Framework 3.5 aus.

  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 Code in Program.cs durch den Code am Ende dieses Verfahrens.

  8. Speichern Sie das Projekt.

  9. Ersetzen Sie die Werte von strModelName und strXmlFile2Import entsprechend mit dem Namen und vollständigen Pfad Ihrer Modelldatei.

  10. Ersetzten Sie den Wert von strSomeSite mit der URL einer Website in Ihrer Farm.

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

  12. Öffnen Sie Internet Explorer, und navigieren Sie zur Zentraladministration der Farm.

  13. Klicken Sie im Browser auf Dienstanwendungen verwalten.

  14. Klicken Sie auf Business Data Connectivity-Dienst.

    HinweisHinweis

    Business Data Connectivity Service ist der Standardname des BDC-Diensts. Wenn der Administrator den BDC-Dienst anders benannt hat, wird ein anderer Name auf der Webseite der Zentraladministration angezeigt.

    Das gerade importierte Modell wird angezeigt.

using System;
using System.IO;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.BusinessData.Administration;
using Microsoft.SharePoint.BusinessData.Parser;
using Microsoft.SharePoint.BusinessData.SharedService;

namespace Microsoft.SDK.Sharepoint.Samples
{
    class Program
    {
        static void Main(string[] args)
        {
            # region declarations and initializations

            // The model name is used to compose its file name 
            // and also to check hits existence in the catalog.
            string strModelName = "testBCSimport";

            // The full path to the XML model to import.
            string strXmlFile2Import = @".\" + strModelName + ".xml";

            // A site in the farm is used as a reference point 
            // to the farm.
            string strSomeSite = "http://intranet.contoso.com/";

            // The model definition file is the XML data to import.
            string strXmlData2Import;

            // An array to gather the non-critical errors encountered 
            // during the import process.
            string[] strarrNoncriticalErrors;

            // Specify what to import: {"All", "InlineProxies", 
            // "LocalizedNames", "Model", "None", "Permissions", 
            // "Properties"}.
            PackageContents packageContentsImportFlags = 
                PackageContents.All;

            // Setting to use when obtaining resources to 
            // import the model.
            string strResourcesSettingId = "";

            // If blUpdateExistingModel is true, the model definition 
            // that was already in the database is updated 
            // to contain only the external content types 
            // in the given XML. 
            // This method throws an exception if the model 
            // is not in the database. 
            // If blUpdateExistingModel is false, the model is
            // created for the first time, 
            // and this method throws an exception if the 
            // model already exists in the database. 
            // When the model is being updated, the external 
            // content types that were previously in the model 
            // are not deleted. 
            //   The caller must clean them up separately.
            bool blUpdateExistingModel = false;

            // A GUID that is used to track this import process.
            Guid guidJobId = 
                new Guid("C6E88A92-31C2-4D02-9890-5DC2ADB36EA9");

            # endregion declarations and initializations

            # region import

            try
            {
                // Get the model.
                strXmlData2Import = File.ReadAllText(strXmlFile2Import);

                // Identify the farm using a site in the farm.
                using (SPSite site = new SPSite(strSomeSite))
                {
                    // Reference the farm to host the BCS definitions.
                    using (new Microsoft.SharePoint.SPServiceContextScope(
                        SPServiceContext.GetContext(site)))
                    {
                        // Reference the BDC service.
                        BdcService service = 
                            SPFarm.Local.Services.GetValue<BdcService>
                            (String.Empty);

                        // Get the catalog of the referenced BDC service.
                        AdministrationMetadataCatalog cat = 
                            service.GetAdministrationMetadataCatalog(
                            SPServiceContext.Current);

                        // Import the XML definition.
                        Model bcsadminModel = cat.ImportPackage(
                            strXmlData2Import,
                            out strarrNoncriticalErrors,
                            packageContentsImportFlags,
                            strResourcesSettingId,
                            blUpdateExistingModel,
                            guidJobId);

                        Console.Out.NewLine = "\n\r\n\r";
                        int iNumOfNoncriticalErrors = 
                            strarrNoncriticalErrors.Length;
                        if (iNumOfNoncriticalErrors > 0)
                        {
                            Console.WriteLine("Noncritical Errors");
                            for (int iCtr = 0; 
                                iCtr < iNumOfNoncriticalErrors; 
                                iCtr++)
                            {
                                Console.WriteLine(
                                    strarrNoncriticalErrors[iCtr]);
                            }
                        }
                        Console.WriteLine(
                            "Import completed successfully");
                        // Now go to the farm's central administration, 
                        // click Manage service applications,
                        // click Business Data Connectivity 
                        // (the name of the BDC service) 
                        // and enjoy the newly imported model.
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Console.Read();
            }

            # endregion import
        }
    }
}

Siehe auch

Referenz

PackageContents

BdcService

Services

AdministrationMetadataCatalog

GetAdministrationMetadataCatalog(SPServiceContext)

ImportPackage(String, [], PackageContents, String, Boolean, Guid)

Model