Showing posts with label Business Data Catalog. Show all posts
Showing posts with label Business Data Catalog. Show all posts

Thursday, April 17, 2008

Programatically using the Business Data Catalog

Yesterday I had to convert an existing SharePoint WebPart from using Microsoft CRM webservices to use the MOSS Business Data Catalog (BDC) as the means of data connection.

I created the following class that may be of use to some people. It builds up a string dictionary of the entity you are searching for. For example if you need to find all the information about a particular organisation pass in the name of the entity "dboAccountBase", the field to search on "AccountID", and the Guid for the organisation. You will be returned a string dictionary containing all the fields for your particular organisation.

 
private Dictionary GetDictionary(string entityName, string filterKey, string filterValue)
{
Dictionary dict = new Dictionary();

Entity entity = BdcInstance.GetEntities()[entityName];
FilterCollection filters = entity.GetFinderFilters();
IEntityInstanceEnumerator enumerator = entity.FindFiltered(filters, BdcInstance);

while (enumerator.MoveNext())
{
IEntityInstance iEntity = enumerator.Current;
DataRow dr = iEntity.EntityAsDataTable.Rows[0];

if ((dr[filterKey] != null || dr[filterKey] != DBNull.Value)
&& (dr[filterKey].ToString().ToUpper() == filterValue.ToUpper()))
{
foreach (DataColumn dataColumn in dr.Table.Columns)
{
if (dr[dataColumn] == null || dr[dataColumn] == DBNull.Value)
{
dict[dataColumn.ColumnName] = string.Empty;
}
else
{
dict[dataColumn.ColumnName] = dr[dataColumn].ToString();
}
}
break;
}
}
return dict;
}




private LobSystemInstance _instance = null;


private LobSystemInstance BdcInstance
{
get
{
if (_instance == null)
{
NamedLobSystemInstanceDictionary sysInstances =
ApplicationRegistry.GetLobSystemInstances();

_instance = sysInstances[BdcInstanceName];
}
return _instance;
}
}