Code Snippet: Get the DatabaseBackedMetadataCatalog on the Server
Applies to: SharePoint Server 2010
In this article
Description
Prerequisites
To use this example
Description
The following code example shows how to get the DatabaseBackedMetadataCatalog on the server by using the Business Data Connectivity (BDC) service Runtime object model on the server.
Prerequisites
Microsoft SharePoint Server 2010 or Microsoft SharePoint Foundation 2010 installed on the server
Microsoft .NET Framework 3.5 and Microsoft Visual Studio
At least one external content type registered in the BDC Metadata Store
To use this example
Start Visual Studio, and then create a new C# Console application project. Select .NET Framework 3.5 when you create the project.
From the View menu, select Property Pages to bring up the project properties.
On 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 (from SharePoint_RootFolder\ISAPI)
Microsoft.SharePoint
Replace the auto-generated code in Program.cs with the code listed at the end of this procedure.
Replace the placehoder values of <siteUrl>, <nameSpace>, <lobsystemInstance>, and <entityName> with valid values.
Save the project.
Compile the project.
Run the application on the server computer where SharePoint Server 2010 or SharePoint Foundation 2010 is installed.
using Microsoft.BusinessData.MetadataModel;
using Microsoft.BusinessData.Runtime;
using Microsoft.SharePoint.BusinessData.MetadataModel;
using Microsoft.SharePoint;
using Microsoft.SharePoint.BusinessData.SharedService;
//Code.
// Get context from specific site.
SPServiceContext context = SPServiceContext.GetContext(new SPSite("<siteUrl>"));
IMetadataCatalog databaseCatalog = null;
BdcService bdcService = SPFarm.Local.Services.GetValue<BdcService>(String.Empty);
if (null != bdcService)
{
databaseCatalog = bdcService.GetDatabaseBackedMetadataCatalog(context);
}
string @namespace = "<nameSpace>";
string entityName = "<entityName>";
string lsiName = "<lobsystemInstance>";
IEntity entity = databaseCatalog.GetEntity(@namespace, entityName);
ILobSystem lobSystem = entity.GetLobSystem();
ILobSystemInstance lsi = lobSystem.GetLobSystemInstances()[lsiName];