Tuesday, November 24, 2009

SharePoint 2010: Business Connectivity Service Part I

At this point there is still a lot of confusion surrounding the name of the new Business Data Catalog. In SharePoint 2007 it was Business Data Catalog in 2010 it’s Business Data Catalog or Business Connectivity Services or External Content Types. In Visual Studio 2010 it’s still Business Data Catalog and in SharePoint Designer 2010 it’s called External Content Types, but what’s in a name? I’ll call them Business Connectivity Services.

In SharePoint 2007, the BDC lacked any form of UI, but in 2010 that has changed! You can find the Business Connectivity Services under: Central Administration –> Manage Service Applications –> Business Data Catalog. My preferred weapon of choice is Visual Studio, but if you want to do the basics then you can use SharePoint Designer (it’s a lot friendlier). When you’re trying to integrate with a very basic WCF service or SQL database or even a .dll (!!) SharePoint Designer is the way to go. For all your other services (or custom work) use Visual Studio.

For this first part I would like to do a basic thing. Staying close to the old BDC way. The BDC works with a Finder and a Specific Finder method. The Finder method returns a list of arbitrary items and the Specific Finder returns an item based on the ID of that item. I also want to do some custom work, so I won’t be creating my own webservice that has those two (Finder, Specific Finder) methods, that would be too easy.

I found a webservice at: http://www.webservicex.net/country.asmx that returns a string, but the string is an XML DataSet representation of a list of countries. If you try to set this thing up with the old BDC you’ll only get one object with a string in it. In the new BCS you now have the ability to customize the Finder and Specific Finder methods.

First you have to install the Microsoft Visual Studio 2010 tools for SharePoint 2010. Start Visual Studio 2010 and create a new Business Data Catalog model from the SharePoint 2010 projects.

VSTS BDC

The Business Data Catalog Model starts with a basic model with Entity1 and an Entity1Service. If you run this project and create an external list in SharePoint, you’ll see your first BDC application running in SharePoint.

Visual Studio now has a great new designer for the BDC Model and it’s a good one. Another good thing is, is that everything is XML. You can edit the DataCatalog files in the XML editor, I don’t recommend it, but you can. Stay in the BDC Explorer for now, it has everything we need to do. Go to the Solution Explorer and we’ll start by renaming the Entity1 to Country and remove all of the properties of it. Add three new properties to Country: IsoCode, Name and Currency. Add a Web Service Reference to the project for http://www.webservicex.net/country.asmx. Go back to the BDC Explorer and open BusinessDataCatalog1. You will see that there is still an Entity1 in there, rename it to Country. Rename Identifier1 to IsoCode and rename the methods to FindAllCountries and FindCountryByIsoCode (Finder and Specific Finder). I found a bug in this release; whenever you rename a method in the BDC designer it automatically creates a new method in your service. I hope they’ll fix this in the Beta1.

In the BDC Method Details you’ll see all the details of all the methods. You’ll find that there will still be references to Entity1 or Identifier1. You can change these to Country and IsoCode respectively. One thing you should realize is that you’re now creating a model on top of the Country and CountryService classes. Make sure that all the names and objects in your model have underlying C# properties and classes. If you select the FindCountriesByIsoCode method in the BDC Explorer, you can open the combobox for Country (or Entity1 if you forgot to rename it) next to returnParameter and click on Edit. You’ll jump to the BDC Explorer and see the Model version of Country. Make sure that all the properties of Country are in the Model version, with the right name and type name. You can add “Properties” by right clicking on Country and select “Add type descriptor”.

When you’re done, your BDC Model should look something like this:

BDC details

If you’ve done all of this correctly you can go to the CountryService.cs (in the Solution Explorer) and start coding on the FindAllCountries and FindCountryByIsoCode methods. When you’re all done do not forget to copy your serviceModel settings (app.config) to the SharePoint web.config file. (The automatic deployment in Visual Studio works great, but it forgets configuration settings). If you’ve done all of this correctly, then simply press F5 and navigate to your external content list and you’ll see this:

countrylist in sharepoint

1 comment:

  1. I'm trying to create a application def file for MOSS2007 using the MS Application Definition Designer and the the webservice http://www.webservicex.net/country.asmx

    When trying to execute the method instance of the GetCountries method, of type finder, within the Application Definition Designer, I receive the error, "Back-end system adapter returned a structure incompatible with the corresponding metadata"

    I understand the meaning of the error but don't know how to resolve it. I'm hoping for a point in the right direction.

    ReplyDelete