代码段:从 BDC 元数据存储区导出 BDC 模型

上次修改时间: 2010年5月13日

适用范围: SharePoint Server 2010

本文内容
说明
先决条件
使用此示例

说明

以下示例演示如何从元数据存储区导出 BDC 模型。

先决条件

  • Microsoft SharePoint Server 2010 或 Microsoft SharePoint Foundation 2010 位于服务器上。

  • Microsoft .NET Framework 3.5 位于客户端计算机上。

  • Microsoft Visual Studio。

使用此示例

  1. 启动 Visual Studio 并创建 C# 控制台应用程序项目。创建项目时选择".NET Framework 3.5"。

  2. 从"视图"菜单中,单击"属性页"以显示项目属性。

  3. 在"生成"选项卡中,为"目标平台"选择"任何 CPU"。

  4. 关闭项目属性窗口。

  5. 在"解决方案资源管理器"中的"引用"下,删除除 System 和 System.Core 之外的所有项目引用。

  6. 向项目中添加以下引用:

    1. Microsoft.BusinessData

    2. Microsoft.SharePoint

    3. System.Web

  7. 用此过程结尾处列出的代码替换 Program.cs 中自动生成的代码。

  8. 将"<siteUrl>"字符串值替换为有效的 SharePoint 网站 URL。

  9. 保存该项目。

  10. 编译并运行该项目。

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

        }
    }
}

请参阅

引用

BdcService

Services

AdministrationMetadataCatalog

GetAdministrationMetadataCatalog(SPServiceContext)

ModelCollection

AdministrationMetadataCatalog

Model

Name

WriteXml(PackageContents)