Introduction

In this code sample I provide an extension to the standard SharePoint 2010 Business Conectivity Services admin API that helps to create external content types and the related external lists using client side APIs. A console application is used to illustrate the usage of the extension.

Original Situation and Plans

Although SharePoint Designer 2010 and Visual Studio 2010 contains tools that make it easier to create Business Connectivity Services models and extenal content types (ECTs) through their user interface, sometimes it is useful to achieve that from your custom code. Such case might be a custom tool that creates ECTs dynamically, for example based on a database schema or an XSD file.

Todd Baginski wrote a blog post about how to use the BCS admin API to create ECTs from custom code. It's great to learn the API, but I found it tedious and error-prone, so I suggest to make some improvements if you would like to work with the API in a production code.

Some of the limitations I found in the original API:

These issues make it harder to create a maintainable code.

Solution

In the sample solution I addressed the limitations via extension methods, reflection calls and by introducing a helper class to store aggregated method call parameters. I also illustrate how to check the validity of the ECT before trying to create it.

Since I was to provide a complete solution to deploy the ECT from client side including creation of an external list for it, the sample contains the code that checks the existence of the external SharePoint list, removes and recreates it, all of these using the SharePoint 2010 Managed Client Object Model, so no SP 2010 server side assemblies required, one can use the tool remotely.

Using the Sample

The sample consists of a single-project Visual Studio 2010 solution that builds as a console application.

There are three code files in the project:

To run the sample you need Visual Studio 2010, administrative access to a SharePoint Foundation 2010 or SharePoint Server 2010 and an installed AdventureWorks database.

Before running the code, alter the server and database names where it is signed in the code by the TODO comment.

Limitations, Assumptions

In the example I assume that SP2010 and the AdventureWorks database are located on the same server. Otherwise further security configurations required.

Further Details

You can read more about the individual elements of the solution in these posts:

A more developer-friendly way of programmatically creating SharePoint 2010 external content types

How to check if a specific list or folder exists using the managed client object model

Creating external lists using the managed client object model