Business Connectivity Services in SharePoint 2013 – Using The Client Object Model

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.

  1. Prerequisites:
    • Server machine with installed Microsoft SharePoint 2013 Preview
    • Client machine with installed Microsoft Visual Studio 2010 Professional or higher
  2. 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).
    Create External Content Type
  3. On a client machine open Visual Studio 2010
  4. Add a New Project
  5. Expand the “Visual C#/Windows” node, select “WPF Application” project type then
  6. Give your project a name i.e “ClientObjectModelBCSDemo” and click “OK”
    Create Visual Studio Project
  7. Make sure that you are using .NET Framework 4.0
    Select .NET Framework version 4
  8. 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.
    Add SharePoint Client Assemblies
  9. 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.
    SharePoint Assemblies in Project
  10. 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.
    Setup WPF Layout
  11. 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}”/>
  12. 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;
  13. 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>”;
  14. 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))));
    }
    }
  15. 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; }
    }
  16. Your code-behind class file should look like this
    Code behind file
  17. 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.
    BCS data in client application
  18. Now let’s create an External List on the SharePoint page based on the same External Content Type.
    Create external list
  19. You may see that we have the same data.
    Display external list 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/>