SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 webparts but i want to create custom webpart so i need to call finder method in webpart programmatically in bcs i have finder method with three parameters username,password and currentUser.How can i call finder method programmatically with these 3 parameters.

Please advise Thanks Ronak

share|improve this question
up vote 2 down vote accepted
//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

share|improve this answer
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 Jul 9 '12 at 14:54

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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