Compartilhar via


Code Snippet: Export a BDC Model From the BDC Metadata Store

Applies to: SharePoint Server 2010

In this article
Description
Prerequisites
To use this example

Description

The following example shows how to export a BDC model from the Metadata Store.

Prerequisites

  • Microsoft SharePoint Server 2010 or Microsoft SharePoint Foundation 2010 on the server.

  • Microsoft .NET Framework 3.5 on the client computer.

  • Microsoft Visual Studio.

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 autogenerated code in Program.cs with the code listed at the end of this procedure.

  8. Replace the "<siteUrl>" string value with a valid SharePoint site URL.

  9. Save the project.

  10. Compile and run the project.

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

namespace Microsoft.SDK.Sharepoint.Samples
{
    class Program
    {
        static void Main(string[] args)
        {      
            # region export
            using (SPSite site = new SPSite(<siteUrl>))
            {
                using (new Microsoft.SharePoint.SPServiceContextScope(SPServiceContext.GetContext(site)))
                {

                    if (SPFarm.Joined == false) throw new Exception("You are not running in a server in a SharePoint Farm");
                    
                    BdcService service = SPFarm.Local.Services.GetValue<BdcService>(String.Empty);
                    AdministrationMetadataCatalog catalog = service.GetAdministrationMetadataCatalog(SPServiceContext.Current);

                    // Model names could be long and complex, getting and listing all models.
                    ModelCollection models = catalog.GetModels("*");
                    
                    Console.WriteLine("ID" + "\tModel Name");
                    foreach (Model m in models)
                    {
                        Console.WriteLine(m.Id + "\t" + m.Name);                 
                    }

                    // Let the user choose the model by ID.
                    Console.WriteLine("Type the ID of the Model to export");
                    uint modelToExport = uint.Parse(Console.ReadLine());
                    
                    foreach (Model m in models)
                    {
                        //Make sure the model is there by ID
                        if (m.Id == modelToExport)
                        {
                            Console.WriteLine("Writing model to:");
                            Console.WriteLine(m.Name + ".xml");
                            //Export the model with all contents to model name file with XML extension.
                            System.IO.File.WriteAllText(m.Name + ".xml", m.WriteXml(Microsoft.SharePoint.BusinessData.Parser.PackageContents.All), System.Text.Encoding.Unicode);                        }
                    }
                }
            }
            # endregion export

        }
    }
}

See Also

Reference

BdcService

Services

AdministrationMetadataCatalog

GetAdministrationMetadataCatalog(SPServiceContext)

ModelCollection

AdministrationMetadataCatalog

Model

Name

WriteXml(PackageContents)