Académique Documents
Professionnel Documents
Culture Documents
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.
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!
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:
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!
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.
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
/w EPDw UBMA9k
Home Contact
About Tobias
Tobias
Easily Free
Zimmergren's
Secure SharePoint
thoughts
SharePoint
on
Monitoring
development
Documents Tool
Monitor
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,
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
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!
Open the site you want to work with using SharePoint Designer 2010
Select
"External
Content
Types"
in
the
left
hand
navigation:
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!
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.
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.
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:
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