Compartilhar via


Code Snippet: Import a BDC Model Into the Metadata Store

Applies to: SharePoint Server 2010

The following example shows how to import a BDC model into a farms catalog.

Prerequisites:

  • Microsoft SharePoint Server 2010 or Microsoft SharePoint Foundation 2010.

  • Microsoft .NET Framework 3.5.

To use this example

  1. Start Visual Studio 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.BusinessData

    2. Microsoft.SharePoint

    3. System.Web

  7. Replace the code in Program.cs with the code listed at the end of this procedure.

  8. Save the project.

  9. Replace the values of strModelName and strXmlFile2Import with the name and full path of your model file, respectively.

  10. Replace the value of strSomeSite with the URL of a site in your farm.

  11. Compile and run the project.

  12. Open Internet Explorer and navigate to the farm's central administration.

  13. In the browser, click Manage service applications.

  14. Click Business Data Connectivity Service.

    Note

    Business Data Connectivity Service is the default name of the BDC service. If the administrator has named the BDC service differently, a different name will appear in the Central Administration Web page.

    The newly imported model is displayed.

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
        }
    }
}

See Also

Reference

PackageContents

BdcService

Services

AdministrationMetadataCatalog

GetAdministrationMetadataCatalog(SPServiceContext)

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

Model