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
Start Visual Studio and create a C# Console application project. Select .NET Framework 3.5 when you create the project.
From the View menu, click Property Pages to bring up the project properties.
In the Build tab, for the Platform target, select Any CPU.
Close the project properties window.
In Solution Explorer, under References, remove all project references except for System and System.Core.
Add the following references to the project:
Microsoft.BusinessData
Microsoft.SharePoint
System.Web
Replace the autogenerated code in Program.cs with the code listed at the end of this procedure.
Replace the "<siteUrl>" string value with a valid SharePoint site URL.
Save the project.
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
}
}
}