Vous êtes sur la page 1sur 36

SP 2010: Getting started with Business Connectivity Services (BCS) in SharePoint 2010

Author: Tobias Zimmergren http://www.zimmergren.net | http://www.tozit.com | @zimmergren

Introduction
Article 1 in the small BCS-series:

1. 2.

SP SP

2010: 2010:

Getting

started

with work

the with

Business External

Connectivity (BCS) in

Services SharePoint

(BCS) 2010

Programmatically

Lists

3. SP 2010: Programmatically work with External Lists (BCS) using the Client Object Model

BCS in SharePoint 2010 is an awesome refinement of the Business Data Catalog from MOSS 2007. With BCS - or Business Connectivity Services - you get the possibility to connect external data and work with it from SharePoint. In this article I will not cover the basics of what BCS is all about (MSDN/TechNet does this very well) - I will rather give you a walkthrough of how you can setup a BCS connection to an external database, and then work with this information directly from a SharePoint list - without the user actually knowing anything about the connection to the database.

A sample SQL database


I'll just show you how my sample database is set up - simply create a new database in your SQL Server and have it filled with some example data. In my case, this is the data in my SQL

database,calledZimmergren_DB:

In this sample database, I've added a table called ProductList which in theory will represent some products in this database, like this:

I'm filling the database with some sample data, so we will be familiar with this data when we later watch this information from SharePoint:

Alright - we have some sample data in our SQL Server. Nothing fancy, just some very simple data. Great, let's get going with the fun stuff!

Creating an external content type

The most effective and easy way to set up a simple BCS connection is to use SharePoint Designer 2010. You heard me, we can now get up and running with BCS by using SPD instead of modeling complex ADF files and things like that. In order to do this, we need to create a new External Content Type! Here's how do create our External Content Type and hook it up with our database, step by step:

Open the site you want to work with using SharePoint Designer 2010 Select
"External Content Types" in the left hand navigation:

Loading this page might take some time, be patient!

Click

to

create

new

External

Content

Type

like

this:

Click the link that reads: "Click here to discover external data sources and define
operations":

Click

"Add

Connection"

Select

"SQL

Server"

as

your

Data

Source

Type:

Enter

the

details

about

your

connection

to

your

SQL

Server:

When the connection is made, your Data Source Explorer will be filled with the
database you have specified. Now choose the table you want to work with, and rightclick and select "Create All Operations":

You'll be presented with a wizard-like dialog where you can specify the operations, elements and other properties for your BCS connection.

Click "Next" to get to the Parameters page Select the field that you want to act as an Identifier. In my case I've selected my
ProductID just to get on with it:

Click "Finish" You'll be presented with a list of operations that your External Content Type can do, like
this:

That's it. A few points, a few clicks - and you're done. Let's create an external list (using the Browser to show how simple it is..) and hook up our external content type with it!

Creating an external list


There's a few ways to create an external list in SharePoint 2010. We will create it using the Browser UI to show you how simple it can be.

Open

your

site

and

choose

Site

Actions

More

Options

Select

the

External

List

template,

and

click

Create

Enter a name for your list, e.g. Product List You'll see a field in this list called External Content Type, click the browse-button
beside it:

What is really awesome here, is that you're now presented with a dialog where you simply can choose the data source for this list. That means, you'll select the data source you've created (mine is called Zimmergren_DB). Then your list will automatically work against the SQL database, but still have the look and feel of a SharePoint 2010 list.

Select

your

data

source

and

click

OK:

Now

simply

click

the

button

called

Create:

Would you look at that! You're now working with external data, from your (what looks to be) normal SharePoint list! This is brilliant! You now have the ability to create new items, update existing items, delete items and do all your normal CRUD-operations (CRUD = Create, Read, Update, Delete) straight from the SharePoint 2010 list.

Proof of concept - Adding a new product

Let's just for the fun of it add a new product called "Awesome Product 1.0" like the following screenshot:

Now go to your SQL Server and see the changes take effect immediately. The data is NOT stored in SharePoint, it's stored in your SQL Database. This is what my table now looks like in the SQL Server, after adding a new item in the SharePoint list:

Summary
With a few points, followed by a few clicks - you've set up your external data connection. Basically it's that simple.

42271097714 none

False 0x68F81BE0C3F0 Brow se

Brow se /w EWCALw npqM

false

/w EPDw UBMA9k

Home Contact

About Tobias

Tobias
Easily Free

Zimmergren's
Secure SharePoint

thoughts
SharePoint

on
Monitoring

development
Documents Tool
Monitor

Microsoft MVP, SharePoint


Based on Metadata. By Titus Labs. With E-mail by Idera. Download it now! Alerts, Performance

Free
Take Sync SharePoint to Laptop!

SharePoint
SharePoint

Reader
Offline

Chat with me if I'm online! search blog most popular recent posts SharePoint Evolution Conference, London SharePoint 2010 - The Developer Tour (Sweden) SP 2010: SharePoint Server 2010 - Creating a custom Document ID provider SP 2010: Site Collection Keep-Alive Job now available by AC SP 2010: Certification exam details for SharePoint 2010 published tag cloud

.NET 3.5, 2010, AJAX, BCS, BDC, Blogging, Branding, CAML, Certifications, Client Object Model, Custom Search, Forum Web Part, HitHighlighting, How To, Indexing, Links, LINQ, LINQ to SharePoint,

MOSS 2007, Queries, Search, Search Query Object Model, Search Results Page, Search Scope, SharePoint, SharePoint Designer, SharePoint Online,
Master Page, MCTS, Microsoft Online Services,

Silverlight, SP2010, Sweden SharePoint User Group, Tips, Tools, Training, UserControls,

WSS 3.0, XSLT,


archives September 2006 (3) October 2006 (2) November 2006 (2) December 2006 (2) January 2007 (1) February 2007 (7) March 2007 (1) April 2007 (4) May 2007 (1) June 2007 (4) December 2007 (2) August 2007 (3) September 2007 (2) October 2007 (2) November 2007 (2) January 2008 (3) March 2008 (10) February 2008 (1) April 2008 (9) May 2008 (5) June 2008 (2) July 2008 (4) August 2008 (2) September 2008 (4) October 2008 (6) November 2008 (8) December 2008 (1) January 2009 (6) February 2009 (7) March 2009 (5) April 2009 (9) May 2009 (5) June 2009 (6) July 2009 (7) August 2009 (6) September 2009 (3) October 2009 (3) November 2009 (2) December 2009 (3)

January 2010 (7) February 2010 (2) March 2010 (6) April 2010 (3) blogroll MVP: Liam Cleary MVP: Ton Stegeman MVP: Todd Baginski MVP: Nick Swan MVP: Jan Tielens MVP: Heather Solomon Joel Oleson MSFT: Pontus Haglund MVP: Vincent Rothwell MVP: Zlatan Daniel McPhearson Mark Schneider MSFT: Team Blog MVP: Andrew Woody MVP: Andrew Connell MVP: Daniel Brown MVP: Eric Schupps MVP: Spence Harbar MVP: Muhanad Omar MVP: Waldek Mastyrkarz

Goodies Easily Secure SharePoint Documents Based on Metadata. By Titus Labs. Free SharePoint Monitoring Tool With E-mail Alerts, Performance Monitor by Idera. Download it now! Free SharePoint Reader Take SharePoint Offline Sync SharePoint to Laptop! Backup SharePoint. Free Trial. High performance SharePoint backup and recovery.

SP 2010: Getting started with Business Connectivity Services (BCS) in SharePoint 2010
Author: Tobias Zimmergren http://www.zimmergren.net | http://www.tozit.com | @zimmergren

Introduction
Article 1 in the small BCS-series:

1. 2.

SP SP

2010: 2010:

Getting

started

with work

the with

Business External

Connectivity (BCS) in

Services SharePoint

(BCS) 2010

Programmatically

Lists

3. SP 2010: Programmatically work with External Lists (BCS) using the Client Object Model

BCS in SharePoint 2010 is an awesome refinement of the Business Data Catalog from MOSS 2007. With BCS - or Business Connectivity Services - you get the possibility to connect external data and work with it from SharePoint. In this article I will not cover the basics of what BCS is all about (MSDN/TechNet does this very well) - I will rather give you a walkthrough of how you can setup a BCS connection to an external database, and then work with this information directly from a SharePoint list - without the user actually knowing anything about the connection to the database.

BCS

Poster:

Business

Connectivity

Services

Poster

BCS Team Blog: http://blogs.msdn.com/bcs/

A sample SQL database


I'll just show you how my sample database is set up - simply create a new database in your SQL Server and have it filled with some example data. In my case, this is the data in my SQL database, called Zimmergren_DB:

In this sample database, I've added a table called ProductList which in theory will represent some products in this database, like this:

I'm filling the database with some sample data, so we will be familiar with this data when we later watch this information from SharePoint:

Alright - we have some sample data in our SQL Server. Nothing fancy, just some very simple data. Great, let's get going with the fun stuff!

Creating an external content type


The most effective and easy way to set up a simple BCS connection, is to use SharePoint Designer 2010. You heard me, we can now get up and running with BCS by using SPD instead of modeling complex ADF files and things like that. In order to do this, we need to create a new External Content Type! Here's how do create our External Content Type and hook it up with our database, step by step:

Open the site you want to work with using SharePoint Designer 2010

Select

"External

Content

Types"

in

the

left

hand

navigation:

Loading this page might take some time, be patient!

Click

to

create

new

External

Content

Type

like

this:

Click the link that reads: "Click here to discover external data sources and define
operations":

Click

"Add

Connection"

Select

"SQL

Server"

as

your

Data

Source

Type:

Enter

the

details

about

your

connection

to

your

SQL

Server:

When the connection is made, your Data Source Explorer will be filled with the
database you have specified. Now choose the table you want to work with, and rightclick and select "Create All Operations":

You'll be presented with a wizard-like dialog where you can specify the operations, elements and other properties for your BCS connection.

Click "Next" to get to the Parameters page Select the field that you want to act as an Identifier. In my case I've selected my
ProductID just to get on with it:

Click "Finish" You'll be presented with a list of operations that your External Content Type can do, like
this:

That's it. A few points, a few clicks - and you're done. Let's create an external list (using the Browser to show how simple it is..) and hook up our external content type with it!

Creating an external list


There's a few ways to create an external list in SharePoint 2010. We will create it using the Browser UI to show you how simple it can be.

Open

your

site

and

choose

Site

Actions

More

Options

Select

the

External

List

template,

and

click

Create

Enter a name for your list, e.g. Product List You'll see a field in this list called External Content Type, click the browse-button
beside it:

What is really awesome here, is that you're now presented with a dialog where you simply can choose the data source for this list. That means, you'll select the data source you've created (mine is called Zimmergren_DB). Then your list will

automatically work against the SQL database, but still have the look and feel of a SharePoint 2010 list.

Select

your

data

source

and

click

OK:

Now

simply

click

the

button

called

Create:

Would you look at that! You're now working with external data, from your (what looks to be) normal SharePoint list! This is brilliant! You now have the ability to create new items, update existing items, delete items and do all your normal CRUD-operations (CRUD = Create, Read, Update, Delete) straight from the SharePoint 2010 list.

Proof of concept - Adding a new product

Let's just for the fun of it add a new product called "Awesome Product 1.0" like the following screenshot:

Now go to your SQL Server and see the changes take effect immediately. The data is NOT stored in SharePoint, it's stored in your SQL Database. This is what my table now looks like in the SQL Server, after adding a new item in the SharePoint list:

Summary
With a few points, followed by a few clicks - you've set up your external data connection. Basically it's that simple.

Working with data in an External List using the SharePoint Object Model
The code in this sample doesn't really differ from the way you fetch information from any other list in SharePoint (2007 or 2010). This - of course - is very welcomed news, as we do not need to learn any new frameworks or tools to work with the data in our external lists. It simply works as any other SPList, basically.

Retrieving external data, made simple:


When fetching items from an external list, you can simply do that by utilizing the good-old SPList object. We do not need to work with any other types of namespaces or frameworks in order to do this. In my SQL Server I've got a table called "ProductList".

This list is filled with the following data:

Fetching some items from the external list, and displaying them in a console app:

//

Product

List

is

my

external

list,

that

is

working

with

data

in

the

SQL

Server! List"];

SPList

list=web.Lists["Product

SPQuery q.Query

new

SPQuery(); =

"<Where><IsNotNull><FieldRef

Name='ProductID'

/></IsNotNull></Where>";

q.RowLimit

100;

SPListItemCollection

col

list.GetItems(q);

foreach

(SPListItem

item

in

col)

Console.WriteLine(item["Name"].ToString());

This will render the following result (fetched from the database):

The things you see in the console windows is fetched straight from the SQL Server (using a BCS connection through the External List).

Writing data to the External List (hence, writing to the SQL Server)
Seriously, this is way too easy as well... // SPList Get list the = external web.Lists["Product list List"];

// Use the traditional approach to create SPListItems and hook it up with the list SPListItem item = list.Items.Add(); item["Name"] = "Sample Product Wohoo";

item["Description"] item.Update();

"Sample

Description

Wohoo";

Upon running this code in your SharePoint application, it will create the SPListItem object and add a Name and Description. When you hit .Update() it will push this data through the data source connection, to your SQL server. Here's what the updated data looks like:

Client Object Model code to read from the external list


Just like I described in my previous article, you now have the awesome ability to work with data in your External Lists (connected to a data source using BCS) - namely, you can use the standard SharePoint APIs. Since I've shown you how you can do this using the Server Object Model, I thought I could take another spin at it and show you the code for doing basically the same with the Client Object Model.

The underlying data


As with my previous article, I'm still using the same data source as I set up in my first article - the "ProductList" table in my SQL Server database called "Zimmergren_DB"

As seen in the SQL Server Management Studio:

Let's fetch the data using a Windows Forms application that utilizes the Client Object Model!
I've designed a Windows Forms application to utilize the .NET Client Object model (in contrary to using the Silverlight client object model or JavaScript client object model). It looks like this:

When you click the fancy button called "Get External Data", it will use the Client Object Model to fetch the records from the external list (from the SQL server) and display them in a DataGridView. Nothing fancy.

The code!
With no further delays or chit-chat, here's the simple code!

// Define the Client Context (as defined in your textbox) SP.ClientContext context = new SP.ClientContext(tbSite.Text); SP.Web site = context.Web; var ProductList = site.Lists.GetByTitle(tbList.Text); SP.CamlQuery camlQueryAwesomeness = new SP.CamlQuery(); IQueryable<SP.ListItem> productItems = ProductList.GetItems(camlQueryAwesomeness); IEnumerable<SP.ListItem> externalList = context.LoadQuery(productItems); // This is where we actually execute the request against the server! context.ExecuteQuery(); // Retrieve the products from the product list using some fancy LINQ var productListData = from product in externalList select new { // We're never pointing to the field at the 0-index // because it's used by the BDC Identity itself. Hence our elements start at 1. ProductID = product.FieldValues.ElementAt(1).Value.ToString(), ProductName = product.FieldValues.ElementAt(2).Value.ToString(), ProductDescription = product.FieldValues.ElementAt(3).Value.ToString() }; // Simply clear the rows and columns of the GridView gvProducts.Rows.Clear(); gvProducts.Columns.Clear(); // Add the columns we need (ProductID, Name, Description) gvProducts.Columns.Add("ProductID", "ProductID"); gvProducts.Columns.Add("Name", "Product Name"); gvProducts.Columns.Add("Description", "Product Description"); foreach (var product in productListData) { // For each product in the list, add a new row to the GridView

gvProducts.Rows.Add( product.ProductID, product.ProductName, product.ProductDescription ); }

Vous aimerez peut-être aussi