Executing BCS External Content Type Methods in C#

In our previous blog post we have gone through how to use the Business Connectivity Services object model to retrieve information about external content types which have been deployed to SharePoint’s Business Connectivity Services.

In this blog post we will show you how to get the Business Connectivity Services methods collection for an external content type and execute the finder and specific finder methods with Business Connectivity Services object model.

Please follow the steps 1 to 5 that are described in our previous blog post to create a simple Visual Web Part and add appropriate references and using statements.

Connect to BdcService and GetEntities

When you have the visual web part created follow the steps below to get you working with external content type’s methods via Business Connectivity Services object model.

1) Add the following using statement to your visual web part’s source code. This namespace allows us to use KeyValuePair class

using System.Collections.Generic;

2) Change Page_Load method, so now it should call the new method to execute external content type’s methods.

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

3) Now, lets define the new method, which should enumerate the methods of an external content type and executes the finder and specific finder methods.

Here is the code of the new method.

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

 

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

 

    // Get entity with appropriate namespace and name
    IEntity entity = catalog.GetEntity(“http://win-74y1750o0mq”, “Employee”);

 

    // Some heading for the literal control
    Literal1.Text = “<h1>” + entity.Name + ” Methods</h1> “ + “</br>”;

 

    // Some variable for finder and specific finder methods’ records
    int finderMethodRecordsCount = 0;
    string strFirstName = “”;

 

    // Get methods collection
    foreach (KeyValuePair<string, IMethod> method in entity.GetMethods())
    {
        // Show methods name
        Literal1.Text += method.Key + “,”;

 

        // Get current method’s instance
        IMethodInstance methodInstance = method.Value.GetMethodInstances()[method.Key];
        if (methodInstance.MethodInstanceType == MethodInstanceType.Finder)
        {

 

            // Execute finder method
            IEntityInstanceEnumerator ieie = entity.FindFiltered(method.Value.GetFilters(methodInstance), entity.GetLobSystem().GetLobSystemInstances()[0].Value);

 

            // Count records count
            while (ieie.MoveNext())
            {
                finderMethodRecordsCount++;
            }
        }

 

        // Execute specific finder method.
        if (methodInstance.MethodInstanceType == MethodInstanceType.SpecificFinder)
        {
            // Identity value
            int i = 1;

 

            // Create new identity
            Identity identity = new Identity(i);

 

            // Execute specific finder method and get the entity instance
            IEntityInstance entInstance = entity.FindSpecific(identity, entity.GetLobSystem().GetLobSystemInstances()[0].Value);

 

            // Show the first name of the entity instance returned by specific finder
            strFirstName = entInstance[“FirstName”].ToString();
        }
    }

 

    Literal1.Text += “<br/> Finder method’s retrieved records count  = “ + finderMethodRecordsCount.ToString();
    Literal1.Text += “<br/> Specific finder method’s returned instance’s first name is “ + strFirstName;
}

 

We’ll now go through the important lines of code individually so we can explain a little more about them…

4) Firstly we need to get the BdcService, metadata catalog and then external content types via the Business Connectivity Services object model.

In this example we have used Employee external content type with http://win-74y1750o0mq namespace.

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

 

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

 

    // Get entity with appropriate namespace and name
    IEntity entity = catalog.GetEntity(“http://win-74y1750o0mq”, “Employee”);

 

5) When you have the Employee external content type you can loop through all the methods available to you in that ECT

IEntity’s GetMethods function returns
collection of KeyValuePair<string,IMethod>, where the key is the name of the method, and IMethod is the method itself.

    // Get methods collection
    foreach (KeyValuePair<string, IMethod> method in entity.GetMethods())
    {
        // Show methods name
        Literal1.Text += method.Key + “,”;

6) When we loop through the methods available to us we need to check the MethodInstanceType value to figure out what type of method it is

    // Get current method’s instance
    IMethodInstance methodInstance = method.Value.GetMethodInstances()[method.Key];
    if (methodInstance.MethodInstanceType == MethodInstanceType.Finder)
    {

 

7) If this iteration returns the Finder method we should execute it, then we’ll simply loop through the records it returns so we can get a record count.

To execute the finder method we should call IEntity’s FindFiltered method.

First parameter of the FindFiltered method is the collection of the filters which we retrieve if we call the IMethod’s GetFilters method and pass the method instance as a parameter.

The second parameter of the FindFiltered method is the lob system instance, which we can retrieve if we call IEntity’s GetLobSystem, and then call GetLobSystemInstance. After all this is done we can iterate over the enumerator returned and increment our record counter.

    // Execute finder method
    IEntityInstanceEnumerator ieie = entity.FindFiltered(method.Value.GetFilters(methodInstance), entity.GetLobSystem().GetLobSystemInstances()[0].Value);

 

    // Count records count
    while (ieie.MoveNext())
    {
        finderMethodRecordsCount++;
    }

8) If our iteration returns the SpecificFinder method we need to execute it in a slightly different way. This is because the SpecificFinder method is always going to be expecting at last one parameter to be passed in (which will map to the identifier)

We need to create an Identity class instance and pass it as an argument to the IEntity’s FindSpecific method. The FindSpecific method’s first parameter should be identifier, and the second parameter is the lob system instance.

When the FindSpecific method is executed it returns an IEntityInstance instance

In our example we are hardcoding the value of the Identifier we are passing in (int i = 1), and simply getting the value of the field FirstName from the EntityInstance returned.

    // Identity value
    int i = 1;

 

    // Create new identity
    Identity identity = new Identity(i);

 

    // Execute specific finder method and get the entity instance
    IEntityInstance entInstance = entity.FindSpecific(identity, entity.GetLobSystem().GetLobSystemInstances()[0].Value);

 

    // Show the first name of the entity instance returned by specific finder
    strFirstName = entInstance[“FirstName”].ToString();

9)  When you have finished editing the code  press CTRL+F5 to deploy to your SharePoint site.

10) Edit the page of your SharePoint site, and add the Visual Web Part that we have been developing.

You can find it in Custom section.

It should display the external content type’s methods’ names, records count retrieved by the Finder method, and First Name of the employee returned by specific finder method.

BCS object model web part

<hrayr/>