One of the new things about the Client Object Model in SharePoint 2013 is the ability to interact with the Business Connectivity Services (BCS). In this blog post we’ll demonstrate how to pull the external data that is exposed by Business Connectivity Services (BCS) by executing an External Content Type’s finder method (Read List operation) in the client WPF application.
- Prerequisites:
- Server machine with installed Microsoft SharePoint 2013 Preview
- Client machine with installed Microsoft Visual Studio 2010 Professional or higher
- Create an External Content Type (you can do it as described in this blog post Creating an External Content Type with SharePoint Designer 2013) on the SharePoint server. In my example I have one External Content Types (Territory).
- On a client machine open Visual Studio 2010
- Add a New Project
- Expand the “Visual C#/Windows” node, select “WPF Application” project type then
- Give your project a name i.e “ClientObjectModelBCSDemo” and click “OK”
- Make sure that you are using .NET Framework 4.0
- In order to use the Client Object Model in our project, you must set references to two client library assemblies: Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll. These assemblies can be found in the 15 Hive folder: %ProgramFiles%Common FilesMicrosoft Sharedweb server extensions15ISAPI on the SharePoint server. You should copy these assemblies to the remote computer on which you create the client application.
- I put the assemblies to the “D:Work!ClientObjectModelBCSDemoClientObjectModelBCSDemodll” folder of my project on my client machine and added the references to them in Visual Studio.
- In order to display the Line of Business data from the External Content Type in our WPF client application we will add a ListBox control to our main window from the Toolbox. Give ListBox a name i.e. externalDataListBox.
- Set the properties of the created ListBox in xaml file as follows
<ListBox Margin=”12″ Name=”externalDataListBox” FontFamily=”Courier New” FontSize=”14″ ItemsSource=”{Binding ExternalData}”/>
- Now add several “using” directives in the code-behind class file MainWindow.xaml.cs to work with the Client Object Model as follows:
using Microsoft.SharePoint.Client;
using Microsoft.BusinessData.MetadataModel;
using Microsoft.BusinessData.MetadataModel.Collections;
- Add following members to MainWindow class and initialize them properly:
private string userName = “<your user name>”;
private string pwd = “<your password>”;
private string domain = “<your domain>”;
private string siteUrl = “<your domain>;
private string entityName = “<Entity name>”;
private string entityNamespace = “<Entity namespace>”;
private string readListInstanceName = “<Instance name of Finder method>”;
- Add a method that will execute Finder method in the External Content Type and pull the external data:
private void ExecuteFinderMethod()
{
// get client context
ClientContext clientContext = new ClientContext(siteUrl);
clientContext.Credentials = new System.Net.NetworkCredential(userName, pwd, domain);
// retrieve a specified data source entity
Entity entity = clientContext.Web.GetEntity(entityNamespace, entityName);
LobSystem lobSystem = entity.GetLobSystem();
LobSystemInstanceCollection lobSystemInstanceCollection = lobSystem.GetLobSystemInstances();
clientContext.Load(lobSystemInstanceCollection);
clientContext.ExecuteQuery();
LobSystemInstance lobSystemInstance = lobSystemInstanceCollection[0];
// get the filters for given name of method instance
FilterCollection fCollection = entity.GetFilters(readListInstanceName);
// get the Line of Business data
EntityInstanceCollection result = entity.FindFiltered(fCollection, readListInstanceName, lobSystemInstance);
clientContext.Load(result);
clientContext.ExecuteQuery();
foreach (var r in result)
{
externalData.Add(string.Join(“,”, r.FieldValues.Values.Select(v => v.ToString().Trim().PadRight(20))));
}
} - In order to display the data in the list box add the following code:
public MainWindow()
{
InitializeComponent();
DataContext = this;
externalData = new List<string>();
ExecuteFinderMethod();
}private List<string> externalData;
public List<string> ExternalData
{
get { return externalData; }
} - Your code-behind class file should look like this
- Run the application and voila, we now can see the Line of Business data from the External Content Type “Territory” in our client WPF application.
- Now let’s create an External List on the SharePoint page based on the same External Content Type.
- You may see that we have the same data.
We hope this walkthrough will be useful for you. If you have any questions feel free to email them to support@lightningtools.com
<Dmitry Kaloshin/>