The SharePoint 2010 Business Connectivity Services (BCS) are providing an Administration Object Model to manage all kind of BCS objects. You can use the an Administration Object Model to programmatically create BDC models, LOB system and instances, Entities and Methods. The Microsoft SharePoint Designer is using the object model itself to let you generate External Content Types (ECT).
The sample code is this blog entry is a WPF client application which will read all BDC model names and display them in a ListView control. The application allows the user to enter credentials other than the current Windows user:
The starting point to access the BCS data is the AdministrationMetadataCatalog class. This class is part of the Microsoft.SharePoint.BusinessData.Administration.Client.dll library and namespace. You also need a reference to the Microsoft.BusinessData.dll library.
In order to create an instance of the AdministrationMetadataCatalog class with custom credentials we have to call the GetCatalog method. We need to pass a custom authentication provider as parameter to GetCatalog. A custom authentication provider is a class which implements the ILobiAuthenticationProvider interface. The interface is quite simple, it just asks you to return the user ID (including the domain name) and the according password.
Here the code of your custom authentication provider:
1 | internal class SharePointConnection : ILobiAuthenticationProvider |
5 |
public SharePointConnection( string userId, string password) |
11 |
public AuthenticationScheme GetAuthenticationScheme( string server, string serverUrl) |
13 |
return AuthenticationScheme.RunAs; |
16 |
public string GetCookie( string server, string serverUrl) |
18 |
return "BCSCustomAuthenticationProvider" ; |
21 |
public string GetUserId( string server, string serverUrl) |
26 |
public string GetPassword( string server, string serverUrl) |
Here the sample on how to use the provider:
2 | SharePointConnection spc = new SharePointConnection(UserIdText.Text, PasswordText.Text); |
5 | AdministrationMetadataCatalog catalog = AdministrationMetadataCatalog.GetCatalog(SiteURLText.Text, spc); |
7 | ModelsListView.ItemsSource = catalog.GetModels( "*" ).Select(m => m.Name); |
Important: If you create your own WPF application using the BCS Administration Object Model and reference the Microsoft.SharePoint.BusinessData.Administration.Client.dll library, you need to set the Platform Target to "Any CPU" in the application settings page, otherwise the project will not compile.
Download Source-Code