Custom .NET Assembly Connector for SharePoint 2010

One of the great features of SharePoint 2010 is its Business Connectivity Services support. You have an abstraction (really big one) over external data sources like WCF, SQL server and custom .NET types. Basically, you can create lists populated by external data. This is really powerful when it comes to importing results from multiple sytems into SharePoint. I will demonstrate how you can create a custom .NET type which can be used as a data source.

First, start Visual Studio 2010 and create a new SharePoint 2010 Business Data Connectivity Model project .

After that you have to choose how the solution will be deployed – as a sandbox or as a farm. BDC models can only be deployed as a farm solution. The project template includes a sample BDC model, a feature and a package to deploy the solution. Delete the created BDC folder and add a new item – new Business Data Connectivity Model.  Open it and delete all identifiers and methods of the entity which it has created by default. Your solution should look like this:

Now, let’s proceed to entity creation. Open Movie.cs and delete everything inside. Add the following properties:

This is our physical entity. Now we have to create a model for it. Open again MovieBdcModel. An entity model has identifiers and methods. Identifiers define the entity among other entities. This is the entity key. In our situation, this is the Id property. So add a new identifier and name it Id. In the Properties windows change Type Name to System.Int32. Let’s create a few methods. Business Connectivity Services (BCS) use a few method types to distinguish methods’ action:

  • Creator – a method used to create an object
  • Updater – a method used to update an object
  • Deleter – a method used to delete an object
  • Finder – a method used to return a collection of objects
  • Specific Finder – a method used to return a single object

For a complete list of method types, check MSDN.

Open the BDC Method Details window (View -> Other Windows -> BDC Method Details) and select Create Finder Method. This will create a new method, called ReadList. This method has one return parameter with a type descriptor, called MovieList. This is not a real type but a type descriptor. Type descriptors are models of real types which are used within the BDC model.

Select this type descriptor and open the Properties window. Change the Type Name to System.Collections.Generic.IEnumerable`1[[MovieBDCModel.MovieBdcModel.Movie, MovieBdcModel]]. Make sure Is Collection prperty is set to true. Then click on the combobox of the descriptor and select Edit. This will open the BDC Explorer window and select MovieList in the tree. Expand it and select it subtype descriptor, called Movie. Every type descriptor which is a collection must have a subtype descriptor which describes the primitive type of that collection.

Right-click and select Properties of Movie. Change the Type Name to MovieBDCModel.MovieBdcModel.Movie, MovieBdcModel. Now you have to define all properties of that descriptor. Our entity has 4 properties, so we have to add 4 type descriptors to Movie.

Change the Type Name of each so it corresponds to the type of the property. Also you need to set Id as an identifier of the Movie type descptor. Change Identifier Entity and Identifier so that the value of the first is set to Movie (MovieBDCModel.MovieBdcModel.Movie), and the value of the second – to Id.

Now you are done with type descriptors of this method. A Finder method must be accompanied by a Specific Finder method. Because we you create a list you always can see individual items separately. So click on <Add a Method> in the BDC Method Details windows and select Create Specific Finder Method. This automatically creates for you a method, called ReadItem. Note it has two parameters – one input and one return. Select the input parameter and check its type descriptor. Make sure its Identifier property is set to Id and its Type Name is set to System.Int32. This way we state that this parameter is our identifier. Now we have to define the return type descriptor. Because we have already configured the Movie type descriptor we can just copy & paste it.

We have successfully modelled our entity. The only thing left is to create our service which implements entity’s methods. In the BDC model folder you will find a class, called MovieService.cs. It contains method declarations based on our model and sample data. Let’s write our logic.

Done! Let’s deploy the solution.

NOTE: If you are using SharePoint 2010 Foundation you won’t be able to deploy your custom BDC model. This is a limitation of this version. The only workaround is to create a custom event receiver to attach your model. This article describes how you can deal with that limitation.

Open Central Administration and navigate to Manage service applications, Business Data Connectivity Service. You should see your external content type in the list.

Select this item and click on Set Object Permissions in the Ribbon. Write down a user/group and select its permissions. Press OK and return back to the list. Now go to your site and create a new External List, called it Movies. Press the Select External Content Type icon and select our content type. Prsss OK and Create then. If you go to the Lists section, you will find your list there.

You can download the solution from here.