Programmatically work with External Content Types (BDC models) using BCS Object Model

BCS object model becomes handy in situations where we want to build our own web part or web control to display external data using external content types that are already deployed. This article briefly explains and gives you an overview of the BCS Object Model, so that you can quickly jump-start into building custom solutions using BCS Object Model.

As a initial step we need add the references to the relevant assemblies and namespaces in order to make use of the BCS Object model.  Microsoft.BusinessData assembly and the namespaces mentioned below (circled in red) will be used for this purpose.

Having added the relevant assembly and namespaces, I will outline a step by step approach using the Object Model in order to connect and get information from the external content types.

Initially we need to establish a connection and get a reference to BDC Service in SPFarm services, as follows

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

Next step is to get the Metadata catalog which contains the external content types, and this can be achieved by using GetDatabaseBackedMetadataCatalog method of BdcService. This method requires the service context as a parameter, since the code is going to be executed within a visual web part, we can use current service context as below.

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

Once the metadata catalog reference is in place, we can retrieve the particular entity that we are interested in using the GetEntity method of ImetaDataCatalog using the Site URL and the entity name as parameters.

Catalog.GetEntity(site.Url, “MSICNewInformation”);

Note: If we want to get references to all the entities of the deployed external content types, then we can use GetEntities method of ImetaDataCatalog which takes wild card (‘*’) as input parameter.


Having covered the key parts, next we will move into a practical scenario (code given below) where the status of an application is retrieved by passing in the submission date and the number of the application as parameters. Obviously this information is kept in an external database.

As mentioned above we get the reference to the Entity Object, therefore its properties, methods and method instances can be accessed and queries can be executed. In below example we make use of the SpecificFinder method instance of the external content type. 

methodInstance.MethodInstanceType == MethodInstanceType.SpecificFinder

Since the  SpecificFinder method of the external content type returns a single entity instance we make use of the FindSpecific method of Entity object in the BCS Object Model. This method returns single entity instance based on the parameters provided. Parameters are of type Object (which will have the value of the identifier) and LobSystemInstance.

A new object of type Identity is created based on the input parameters (txtdate and txtMsicNumber) and passed to the FindSpecific method along with the LobSystemInstance.

The returned entity instance will have the result filtered based on the input parameters and the status of the application can be retrieved from it.

Reference:         Get Started with Using the Runtime Object Model

Thanks for reading my article and in my next article I will cover some areas in Managed Metadata Services and Taxonomy of SharePoint 2010.


8 comments on “Programmatically work with External Content Types (BDC models) using BCS Object Model

  1. Dipes says:

    I have one project List which is sharepoint BCS list with filter descripter companyID.When i am try to reading project list programetically passing with default value it not passing the value to the readlist .But from view if i pass default value it is working .Please let me know if anything else.

  2. Prashanth says:

    Let me get it clear, are you trying to query the BCS list using CAML?

  3. Dipes says:

    thanks for reply , you are right , through CAML query i am trying to get the list value but to the readlist it is taking as a null value, but from view it is showing the data .

  4. Prashanth says:

    can you post your CAML, and do you get any exception like “The given key was not present in the dictionary”, etc…

  5. Dipes says:

    Sorry I am late , I just put my code to you , if i debug, it is calling readlist methos with blank parameter .

    Please let me know an earlst .Regards,
    #region for BCS
    using (SPSite site = SPContext.Current.Site)

    if (site != null)

    using (SPWeb web = site.OpenWeb())

    SPList list = web.Lists[“Projects”];
    SPListItemCollection listItems = null;
    SPQuery JobQuery = new SPQuery();

    #region This is Company List Query
    string Query = string.Empty;

    #region Privious query
    Query = “”;
    Query += “”;
    Query += “”;
    Query += “”;
    Query += “”;
    Query += “”;
    Query += “”;
    JobQuery.Query = Query;
    // JobQuery.Query = @”
    // NCLm5ylBkQ
    // “;
    // JobQuery.ViewFields=
    // @”
    // “;

    listItems = list.GetItems(JobQuery);

    foreach (SPListItem splistItem in listItems)
    ddlProjectList.Items.Add(new ListItem(splistItem[“PROJECT_NAME”].ToString(), splistItem[“PROJECT_ID”].ToString()));
    web.AllowUnsafeUpdates = false;

    catch (Exception ex)
    { }

  6. Dipes says:

    sorry , Query it did not take ,The filter value currently i am sending by default
    “” Please have a look

  7. You’ve made some decent points there. I checked on the web to learn more about the issue and found most people will go along with your views on this web site.

  8. naiya says:

    i have just followed this step. but i got error like access denied by business connectivity service.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s