Connect to BdcService and GetEntities – BCS Object Model

By

In this walkthrough we will show you how to connect to BCS Service and retrieve basic information about BCS External Content types which have been already deployed.

We will create a visual web part with a very simple user interface, and will use BCS Object Model within that web part’s source code.

1) Open up Visual Studio 2010 and create new Visual Web Part project.

Create SharePoint project

[click the image for a larger view]

2) Edit the user control in the designer and add literal control from the toolbox. We will use this simple user interface and literal control to display some information later.

Add literal control

3) Next we need to add a reference to Microsoft.BusinessData assembly. In the solution explorer right click on the References and select Add Reference.

Add Reference to Project

4) In the Add Reference dialog, go to Browse tab and browse to C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions14ISAPI. Add the Microsoft.BusinessData assembly.

Browse to the SharePoint directory

5) View the code of Visual web part’s user control (VisualWebPart1UserControl.ascx.cs) and add the following namespaces.

using Microsoft.SharePoint;
using Microsoft.SharePoint.BusinessData;
using Microsoft.SharePoint.BusinessData.SharedService;
using Microsoft.BusinessData.MetadataModel;
using Microsoft.BusinessData.Runtime;
using Microsoft.SharePoint.Administration;

6) Add the following method just under the Page_Load method.

private void GetExternalContentTypesAndLobSystems()
{
    // Get reference to BDC Service
   
BdcService service = SPFarm.Local.Services.GetValue<BdcService>();

    // Get MetaData Catalog
   
IMetadataCatalog catalog = service.GetDatabaseBackedMetadataCatalog(SPServiceContext.Current);

    // Some heading for the literal control
   
Literal1.Text = “<h1>Lob Systems and Entities … </h1> “ + “</br>”;

    // Get all the external content types from the meta data catalog
 
   foreach (IEntity ect in catalog.GetEntities(“*”))
    {
        // Add external content type’s name and appropriate lob system name to the literal control
 
       Literal1.Text += ect.Name + “,” + ect.GetLobSystem().Name + “</br>”;
    }

}

We have put some comments in the code which will help you to understand what we have used to a list of external content types, but now we will explain in more details. Methods used in this sample have overloads too, but we will r
efer only to ones which are used in this code snippet.

First of all we need to get reference of BdcService, and we need to use SPFarm’s services for that.

BdcService service = SPFarm.Local.Services.GetValue<BdcService>();

Next, we should get the MetaData catalog which contains our deployed external content types. To do that, we have used BdcService’s GetDatabaseBackedMetadataCatalog method, this expects service context as a parameter. Cause, the code is executed within the visual web part, we can use current service context.

IMetadataCatalog catalog = service.GetDatabaseBackedMetadataCatalog(SPServiceContext.Current);

Once we have the metadata catalog reference, we can retrieve information about the deployed external content types. IMetaDataCatalog interface contains GetEntities method, which expects a wildcard parameter as an input, and based on that parameter it retrieves an enumerable collection of IEntity type objects. We have used “*” parameter to retrieve all the external content types.

foreach (IEntity ect in catalog.GetEntities(“*”))

When we have an IEntity object, we can use its Name property and also call GetLobSystem() method to get the lob system of the external content type.

Literal1.Text += ect.Name + “,” + ect.GetLobSystem().Name + “</br>”;

7) Call the new added method from Page_Load method of the visual web part’s user control.

protected void Page_Load(object sender, EventArgs e)
{
    GetExternalContentTypesAndLobSystems();
}

8) Now, when everything is done, rebuild the project and press CTRL+F5 to deploy the visual web part.

9) Go to your SharePoint site, click on Page ribbon on the top , and then click on Edit button.

 

Edit the page

10) When the page is edited, click on Insert ribbon and then on Web Part button.

Add Web Part

11) In the categories section select the Custom group, and you’ll find VisualWebPart1 there. Add it to page.

Select the Custom group

12) When web part is added to a page, it will execute Page_Load method, which will call our GetExternalContentTypesAndLobSystems method, and appropriate external content types, lob systems names will be displayed.

BCS entities are displayed

 

In the coming posts we’ll be going into much more detail and doing some cool things in C# code with BCS data

<hrayr/>