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.
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.
Thanks for reading my article and in my next article I will cover some areas in Managed Metadata Services and Taxonomy of SharePoint 2010.