上次修改时间: 2010年5月13日
适用范围: SharePoint Server 2010
下面的示例演示如何将 BDC 模型导入到场目录。
先决条件:
Microsoft SharePoint Server 2010 或 Microsoft SharePoint Foundation 2010。
Microsoft .NET Framework 3.5。
使用此示例
启动 Visual Studio 并创建一个 C# 控制台应用程序项目。在创建此项目时,请选择".NET Framework 3.5"。
从"视图"菜单中,单击"属性页"以显示项目属性。
在"生成"选项卡中,为"目标平台"选择"任何 CPU"。
关闭项目属性窗口。
在"解决方案资源管理器"中的"引用"下,删除 System 和 System.Core 之外的所有项目引用。
将以下引用添加到项目中:
Microsoft.BusinessData
Microsoft.SharePoint
System.Web
使用此过程结束时列出的代码替换 Program.cs 中的代码。
保存项目。
将 strModelName 和 strXmlFile2Import 的值分别替换为模型文件的名称和完整路径。
将 strSomeSite 的值替换为服务器场中某网站的 URL。
编译并运行项目。
打开 Internet Explorer 并导航到服务器场的管理中心。
在浏览器中,单击"管理服务应用程序"。
单击"Business Data Connectivity Service"。
备注
Business Data Connectivity Service 是 BDC 服务的默认名称。如果管理员已将 BDC 服务命名为其他名称,则管理中心网页上将显示不同的名称。
显示新导入的模型。
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
}
}
}
请参阅
引用
GetAdministrationMetadataCatalog(SPServiceContext)
ImportPackage(String, [], PackageContents, String, Boolean, Guid)