Hi I have created BCS model which use secure store service for authentication to External system.Everything works fine using out of box external list and BCS web parts.

But I want to create custom web part, so I need to call finder method in web part programmatically. In BCS I have finder method with three parameters username,password and current User.

How can I call finder method programmatically with these 3 parameters?

Please advise.



1 Answer 1

//Connect to the BDC Service Application proxy
BdcService service = SPFarm.Local.Services.GetValue<BdcService>();
if (service == null)
    throw new Exception("No BDC Service Application found.");

//Connect to metadata catalog
DatabaseBackedMetadataCatalog catalog = service.GetDatabaseBackedMetadataCatalog(SPServiceContext.GetContext(site));

//Get entity
IEntity ect = catalog.GetEntity(entityNamespace, entityName);

//Get LobSystem
ILobSystem lob = ect.GetLobSystem();

//Get LobSystemInstance
ILobSystemInstance lobi = lob.GetLobSystemInstances()[lobSystemInstance];

//Get Method Instance
IMethodInstance mi = ect.GetMethodInstance(specificFinderMethodInstance, MethodInstanceType.SpecificFinder);

//Get Parameters
IParameterCollection parameters = mi.GetMethod().GetParameters();

//Set Parameters
object[] arguments = new object[parameters.Count];
arguments[0] = entityInstanceIdentifier;

ect.Execute(mi, lobi, ref arguments);

PropertyInfo[] props = arguments[1].GetType().GetProperties();
PropertyInfo prop = props[0];
SqlDataReader reader = (SqlDataReader)(prop.GetValue(arguments[1], null));
if (reader.HasRows)
    while (reader.Read())
        Console.WriteLine(reader.GetString(3) + " " + reader.GetString(5));

Source: TechNet: Call custom BCS methods from a webpart

  • Thanks Kevin for your reply and code.i do have this code from Scott book and i have used this code to call Finder Method in webparts and it worked.Now i have IEnumerable Items of some entity lets say Workspace now i want to get datatable or collection of objects how can i do that.
    – Ronak
    Commented Jul 9, 2012 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.