Business Connectivity Services (BCS) is a set of out-of-the-box features, services and tools that enhance SharePoint by streamlining the creation of solutions with deep integration of external data and services into SharePoint!
SharePoint 2007 had a similar technology called BDC. But BDC was much more primitive compared to BCS.
BCS, compared to BDC, offers a much richer choice of presentation options, connectivity, a much improved object model, much better tooling support in both SharePoint Designer and Visual Studio 2010, and numerous new and compelling features such as read/write support, external lists, searchability, word controls, etc.
At the heart of BCS is the concept of an external content type. Data in an organization can live in various back-end systems. An external content type is a content type available in SharePoint 2010 that describes the schema and data access capabilities of an external data source and its behavior within Office and SharePoint 2010. Thus, the external content type in BCS is the logical equivalent of what used to be called the Entity in BDC.
You can use Visual Studio 2010 or SharePoint Designer 2010 to design these external content types. In this article, however, I will focus on the Visual Studio 2010 story.
Writing BCS Entities with Visual Studio
Start Visual Studio 2010 and create a new project based on the “Business Data Connectivity Model” project template, and call it “BDCDemo.” This project will generate a collection of BCS external content types. An out-of-the-box entity is created for you that gives you an identifier and two methods. This entity represents the bare minimum definition that you need to create to be able to create an external list from. Once you have created a project based on this project template you will notice three different sections in your Visual Studio project.
- As you can see in Figure 1, on the left you will see a graphical representation of your entity. If you have multiple entities with associations between them they will appeal over here as well.
- In Figure 2, on the right under BDC explorer you will see the definition of the model with the different entities sorted as a tree view.
- And at the very bottom you should see a pane called BDC method details, which shows the various methods and other details that help you design this entity. You will probably end up spending a lot of time in this pane.
You would also note that all three panes are connected with each other. Editing one will reflect the changes in the other two panes. Behind the scenes you’re really editing an xml file. The xml file in this case is the BdcModel1.bdcm file. You could edit the xml file directly as well, but this is a huge improvement over SharePoint 2007 where you didn’t have good tools to edit your xml file that defined the application definition. So chances are that you won’t be editing the xml file manually too much. Also, behind each entity there’s a code behind that is being maintained for you. To view the code behind, select the Entity1 and press F7. Note in the code-behind that there are methods created for you for both read item and read list. The ReadItem is the equivalent of the specific finder method; it returns a single instance of an entity. ReadList, on the other hand, returns a number of instances of the entity. You can see the generated code in Listing 1.
The listing shows that I’m returning an IEnumerable<Entity1>. Also, the out-of-the-box code is creating a single instance of the entity and stuffing it into the return value. Thus, if you were to deploy this external content type and create an external list based on that, that external list will contain a single list item with two columns named Identifier1 and Message, with values 0, and “Hello World” as shown in Figure 3.
If you press F5 to run your project, Visual Studio will package up the BDC entity as a .wsp, deploy it, and since this is a farm solution, modify the web.config to enable debugging and even allow you to step through breakpoints in your code behind.
If you wanted to offer something more powerful, for example, entity instances generated on the fly based on some intelligent logic, you simply have to modify that code-behind!
And when I say modify the code-behind, that really involves two steps:
- First you define the structure of Entity1, maybe perhaps give Entity1 a decent meaningful name. This is done by describing something called as a TypeDescriptor.
- Next you define the details of the ReadList method so that it fills your custom entity structure.
Let’s look at an interesting real-world example. Let’s say that I intend to create an external content type, which when dropped into an external list, will always return the calendar for the current month. This calendar will have three columns: the identifier, the day, and the date.
As I mentioned, the first step is to define the structure of Entity1, which I am going to rename to MonthDay. Also, I will rename Identifier1 to DayNumber, and in the properties, I will change its type to System.Int32. You can see the graphical view of my BDC Model in Figure 1.
So far, so good. Now, click on the ReadItem method. In the bottom pane you can see the Method Details. Look for the ReadItem method and see what parameters it accepts and what parameters it returns! It seems to accept “id” direction “In” and it’s Type Descriptor to be “Identifier1.” Well that’s obviously incorrect!
First of all, what is a Type Descriptor? As the name suggests, it describes a type! A type that is independent of WCF, SQL Server, or .NET connector data types - it is a type that is defined purely in BCS terms, so it is reusable across all those sources. Thus instead of Identifier1, change the input parameter to DayNumber. Also ensure that its data type is now a System.Int32.
Now look at the second parameter called “returnParameter.” Its direction is “Return” but its Type Descriptor is “Entity1.” Again this is incorrect, and you need to ensure that the return type is represented by a Type Descriptor that represents the structure of the information you wish to return. Thus go ahead and set up TypeDescriptor as your return parameter as shown in Figure 4.
In Figure 4, Date is of type System.DateTime, Day is System.String, and DayNumber is System.Int32.
This basically sets up the ReadItem method structure. You still need to provide details of the code-behind! I’ll get to that in a minute, but first let’s also set up the structure for ReadList. For ReadList, create a return Type Descriptor called “MontyDayEntries” and copy/paste the “MonthDayEntry” Type Descriptor under it. Your BDC Explorer should look like Figure 5.
Finally, you need to create a class that represents the structure of MonthDayEntry. I am going to rename Entity1.cs to MonthDayEntry.cs and put code in as shown in Listing 2.
Great, now all that’s left is to populate the code behind’s of ReadItem and ReadList methods, and you will be ready to use this entity within SharePoint. You can find the new code behind in Listing 3.
Go ahead and create an External List called “Days” and use the above created external content type as the source external content type for this list. You should see the list created as shown in Figure 6.
As you can tell, I was writing this in February 2010, and the days of February 2010 are laid out very neatly in this list!
Also, note that since you have not created all the methods on this external content type, the user interface is appropriately trimmed. In addition, in your external list, the “New Item” “Edit Item” and “Delete Item” buttons are grayed out.
Summary
BCS is much much MUCH improved over BDC. I didn’t cover a lot of other enhancements in this article, but in short, let me assert that BCS will get a lot of traction in real-world projects. Using BCS effectively opens you, the architect, up to so many interesting mind-twisting possibilities. And the best part, all this data, is just a list!
Happy SharePointing.