Tuesday, December 9, 2008

Business Data Catalog (BDC) Part - 7

Hi All,

Here i am back with yet another article on BDC series.

suggest to go and read below articles on BDC before proceeding here.

Business Data Catalog (BDC) Part - 1

Business Data Catalog (BDC) Part - 2

Business Data Catalog (BDC) Part - 3

Business Data Catalog (BDC) Part - 4

Business Data Catalog (BDC) Part - 5

Business Data Catalog (BDC) Part - 6

Ok, let’s continue our series of BDC as part 7.

Till now what we are doing was all without writing any code. Now let’s write a code. We may require this if we are not using the BDC Web parts to display the data from BDC Definition. It is like fetching record with the help of ADO.NET and displaying on grid or using the way you like.

So let’s get started.

Here we are going to design a console application to understand each class that is required to connect and methods that are required to access different entities for any application definition.

First I would like to list down the assemblies that we are going to use in this applications.

using Microsoft.Office.Server;
using Microsoft.Office.Server.ApplicationRegistry.Infrastructure;
using Microsoft.Office.Server.ApplicationRegistry.MetadataModel;
using Microsoft.Office.Server.ApplicationRegistry.Runtime;
using Microsoft.Office.Server.ApplicationRegistry.SystemSpecific.Db;

First, we will go forward to using the SSP in which our Definition resides. (Our XML that we imported).

Then you have to set the SSP for using in the application.

string strSSPName = "{ssp_name}";

SqlSessionProvider.Instance().SetSharedResourceProviderToUse(strSSPName);


Then, you need to get the Line of Business objects. Get all definitions in that SSP.

NamedLobSystemDictionary objLOBSysDictionary = ApplicationRegistry.GetLobSystems();

Here what I am doing is making the conditions because I have three definition installed in my machine and I want to give you an example of entities and methods I have all these ready in one of my definitions so I am making conditions. You can write code in your own way.

Ok, now what we will do is we will loop through every BDC Definition and print its name in console.

foreach (LobSystem objLOBSys in objLOBSysDictionary.Values)
{
Console.WriteLine("Name of your BDC Application is : " + objLOBSys.Name + Environment.NewLine);
}

NamedDataClassDictionary objNamedClasses = objLOBSys.GetDataClasses();


Now take out the name of the entities.

foreach (DataClass objClass in objNamedClasses.Values)
{
Console.WriteLine("Entities: " + objClass.Name + Environment.NewLine);
}


Now I am making condition of my own application, you can use your defined application. If you have used the XML that we have discussed so far in previous articles of BDC series, then this approach will be fine for you.

I will take out the Author class and get its methods defined in that.

if (objLOBSys.Name == "Authors")
{
DataClass clsAuthors = objNamedClasses["Author"];

NamedMethodDictionary objMethods = clsAuthors.GetMethods();
foreach (Method objMethod in objMethods.Values)
{
Console.WriteLine("Method: " + objMethod.Name + Environment.NewLine);
}
}
NamedLobSystemInstanceDictionary objInstances = objLOBSys.GetLobSystemInstances();


As we have defined single instance as AuthorTraders, we will get the one single value for this.

foreach (LobSystemInstance objInstance in objInstances.Values)
{
Console.WriteLine("Instance: " + objInstance.Name + Environment.NewLine);
}



Now, let’s get the Author entity.


Entity authorEntity = objInstance.GetEntities()["Author"];

FilterCollection filterColl = authorEntity.GetFinderFilters();


Filter Collection will get all the filters applied on it.

Because we have City as filter so let’s pass it as a parameter.

(filterColl[0] as WildcardFilter).Value = "Oakland";
IEntityInstanceEnumerator authorEnum =

authorEntity.FindFiltered(filterColl, objInstance);
while (authorEnum.MoveNext())
{
DataTable dt =

(authorEnum.Current as DbEntityInstance).EntityAsDataTable;

PrintOnConsole(dt.Rows[0]);

}

private static void PrintOnConsole(DataRow objDR)
{
foreach (DataColumn objColumn in objDR.Table.Columns)
{
Console.Write(objDR[objColumn] + "\t" );
}

Console.Write(Environment.NewLine);
}


Here is the Full Code after explaining all functionality.

namespace BDCConsoleApplication
{
public class Program
{
static void Main(string[] args)
{
string strSSPName = "MATS SSP";

SqlSessionProvider.Instance().SetSharedResourceProviderToUse(strSSPName);

NamedLobSystemDictionary objLOBSysDictionary = ApplicationRegistry.GetLobSystems();

///This Makes the Loop of All Definition installed
///at Farm level that will come as Application Name in objLOBSys.Name

foreach (LobSystem objLOBSys in objLOBSysDictionary.Values)
{

if (objLOBSys.Name == "Authors")
{
Console.WriteLine("________________________________________________________" + Environment.NewLine);
}

Console.WriteLine("Name of your BDC Application is : " + objLOBSys.Name + Environment.NewLine);

///Get data classes method will give us all entities in that application

NamedDataClassDictionary objNamedClasses = objLOBSys.GetDataClasses();

foreach (DataClass objClass in objNamedClasses.Values)
{
Console.WriteLine("Entities: " + objClass.Name + Environment.NewLine);
}


if (objLOBSys.Name == "Authors")
{

DataClass clsAuthors = objNamedClasses["Author"];

NamedMethodDictionary objMethods = clsAuthors.GetMethods();

foreach (Method objMethod in objMethods.Values)
{
Console.WriteLine("Method: " + objMethod.Name + Environment.NewLine);
}

NamedLobSystemInstanceDictionary objInstances = objLOBSys.GetLobSystemInstances();

foreach (LobSystemInstance objInstance in objInstances.Values)
{
Console.WriteLine("Instance: " + objInstance.Name + Environment.NewLine);

Entity authorEntity = objInstance.GetEntities()["Author"];

FilterCollection filterColl = authorEntity.GetFinderFilters();

(filterColl[0] as WildcardFilter).Value = "Oakland";

IEntityInstanceEnumerator authorEnum =

authorEntity.FindFiltered(filterColl, objInstance);

while (authorEnum.MoveNext())
{
DataTable dt =

(authorEnum.Current as DbEntityInstance).EntityAsDataTable;

PrintOnConsole(dt.Rows[0]);

}
}
}
}

Console.ReadLine();

}

///
/// Prints the Record on Cosole Screen
///

///
private static void PrintOnConsole(DataRow objDR)
{
foreach (DataColumn objColumn in objDR.Table.Columns)
{
Console.Write(objDR[objColumn] + "\t" );
}

Console.Write(Environment.NewLine);
}

}


See, we wrote a code without using traditional ADO.Net Approach. This is the other way to get the data from BDC. The power here is that you can actually treat XML as a connection bridge for the database and code and then have flexibility to use it in your own way.

I will continue our series of BDC in my next article.

1 comment:

dotNetFollower said...

Hello!
Thanks for article! I developed a number of classes to simplify getting values from BDC programmatically. You can see them here - SharePoint: How to get value from BDC




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com