My previous post on BCS Walkthrough showed how to make use of the SQL Server Connector, connect to a database,and bring the data back to SharePoint 2010. This used the SQL Server as its data source. If you want to bring the data back from a WCF service, there is the out of the box WCF Service Connector.
What if your external data source is not SQL Server or a web service, and is some custom .NET assembly or a flat file? This is where the .NET Assembly Connectors come into picture. Developers can build & design their custom model & entities using Visual Studio 2010, thereby help the BCS to interact with the custom data sources. The .NET Assembly Connector is nothing but a .NET assembly that acts as a proxy to interact with the external data source.
Lets explore this .NET Assembly Connector with a simple example.
I would also recommend you to watch the BCS Overview Demo videos if you are not familiar with BCS.
External Books List
So, I have this external books list that I want to bring into my SharePoint 2010 site. The list has the following properties associated with it:
- ISBN
- Name
- Author
- Price
- Publisher
BDC Model Project
Open VS2010 and select the Business Data Connectivity Model project template
Once the project is created, you will be presented with the simple entity called Entity1with two read operations:
- ReadList
- ReadItem
The names suggest what they do. ReadList operation is going to return you the full list (list of Entity1) and the ReadItem is going to read a single item (Entity1). You can see the same in the BDC Model Designer:
If you explore the Solution Explorer, you will see the class files and the feature associated with this BDC project
The Entity1 class corresponds to the Entity1 entity and has its properties defined. The Entity1Service class implements the operations, that is ReadList and ReadItem.
Modifying the Entity1 Class
We can now modify the Entity1 class to BookEntity and declare the properties. Rename the Entity1.cs to BookEntity.cs
The ISBN will be our Identifier or the primary key with which a book is identified.
Modifying the Entity1Service Class
Rename the Entity1Service.cs to BookEntityService.cs.
Create a simple books array. For the simplicity of this post we are using Objects. This data can also come from a flat file.
Modify the ReadItem to return the book matching the id, which is the ISBN property:
Modify the ReadList to return the list of all books:
Update the BDC Model Designer
Now that we have modified the code to do what we want, it is time to modify the BDC Designer model. Double click on BdcModel1.bdcm in the Solution Explorer to open the designer. Switch to BDC Explorer.
Go ahead and rename the BdcModel1 to BooksListBdcModel and the BdcModel1 in the LobSystemInstances to BooksListBdcModelInstance
Rename the Entity1 to BookEntity matching our entity name.
In the BDC Model, rename Identifier1 to ISBN
Switch to BDC Explorer and expand ReadItem operation and change the Identifier1 to ISBN under @id
Rename the Entity1 in the @returnParameter to BookEntity and delete the Message type descriptor node. Then rename the Identifier1 to ISBN
Right click on BookEntity node and select Add Type Descriptor. It will add a new type descriptor node:
Rename it to Name.
Do the same and add Author, Price and Publisher. Below is how it will look after adding these properties. Make sure you explore the Properties window and check the type name to be System.String
Expand ReadList operation.You can see that the Entity1 is listed again as the return parameter.
You don’t have to create them again, instead we can copy from the ReadItem operation and paste the BookEntity here. In the ReadItem, right click on BookEntity and select to Copy
Right click on Entity1List and select Paste
You can now safely delete the Entity1 node. Rename the Entity1List to BookEntityList
Below is our modified BDC Model
For more details on these functions, switch to the diagram pane and click on the BookEntity and this should reveal the Method Details Window:
You can see that @id parameter is a parameter that is going to come into the .NET assembly (which is the proxy and is BookEntityService class) and @returnParameter is of return type that this .NET assembly operation is going to return. So now it makes clear that ReadItem is accepting @id parameter which corresponds to the ISBN property (type descriptor) and returns a @returnParameter which is of type BookEntity. The ReadList returns @returnParameter which is of type BookEntityList which is a collection of type BookEntity.
Based on your needs, you can add new methods, define parameters and implement them.
Build and deploy your solution.
Verifying the BDC Model
Browse to your Central Admin and open Application Management | Manage service applications | Business Data Connectivity
You should see the BDC model we just built and deployed
Click on BookEntity to explore the fields and various other properties
Create the External List
Browse to your Site. Click on Site Actions | View All Site Content and then Create
Choose External List from the Content and Data category and click Create
Give a name to your list and choose the BooksListBdcModelInstance as your external content type. You can use the picker to pick
Click Create to create the list.
And here is our list pulling external data from our .NET assembly!
You can download the sample here.