Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Monday, December 05, 2011

Get data from bcs external content type in SharePoint 2010 Programmatically

Here is short example of how to execute a method on an entity Programmatically. Assuming you already have a External content type “My External content type” setup to a Sql database lets look at a console application
private static void ExecuteMethod()
{
Console.WriteLine(“\nNow Executing methods”);
using (SPSite site = newSPSite(siteUrl))
{
using (newSPServiceContextScope(SPServiceContext.GetContext(site)))
{
BdcService service = SPFarm.Local.Services.GetValue();
IMetadataCatalog catalog = service.GetDatabaseBackedMetadataCatalog(SPServiceContext.Current);
IEntity entity = catalog.GetEntity(siteUrl, “My External content type”);
ILobSystemInstance LobSysteminstance = entity.GetLobSystem().GetLobSystemInstances()[0].Value;
IMethodInstance method = entity.GetMethodInstance(“Read List”, MethodInstanceType.Finder);
IEntityInstanceEnumerator handle = entity.FindFiltered(method.GetFilters(), LobSysteminstance);
Console.WriteLine(“Title in My External content type:”);
while (handle.MoveNext())
{
Console.WriteLine(handle.Current["Title"].ToString());
}
}}}

No comments:

Post a Comment