This is the second post in the series about SharePoint BCS.
- SP 2010: Getting started with the Business Connectivity Services (BCS)
- SP 2010: Programmatically work with External Lists (BCS) in SharePoint 2010 (this post)
- SP 2010: Programmatically work with External Lists (BCS) using the Client Object Model
In my previous article, I talked about how you can set up a simple BCS configuration to fetch and work with external data in SharePoint 2010. In this article, I will talk about how you can utilize the SharePoint 2010 object model to work with that external data, directly from the SharePoint API. It’s all farily 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 like 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 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!
SPList list = web.Lists["Product List"];
SPQuery q = new SPQuery();
q.Query ="";
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 are 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)
Similarly, it’s straightforward to write data back to the external list.
// Get the external list
SPList list = web.Lists["Product 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"] = "Sample Description Wohoo";
item.Update();
Upon running this code in your SharePoint application, it will create the SPListItem object and add a Name and Description. Then you make a call to the Update() method it will push this data through the data source connection, to your SQL server.
Here’s what the updated data looks like:
![]()
SharePoint 2010 with BCS External Lists from C# (.net)
We’re running a Beta-product!
There are many new capabilities to use in SharePoint 2010 – where the BCS is one. This article discusses how you can retrieve information from these lists using the normal API approach.
At the time of this writing (during Public Beta), there aren’t any measures on performance and what impact it has on the server in comparison to alternative ways to fetch and work with the data.
As time goes on, there will probably be some new information on this – I’ll keep you posted when I know more.
Summary
As you can see, working with external data from the SharePoint API isn’t very hard to do. What you need to make sure is to have an external list set up somewhere (see this article for how you can do that) and then you can simply use the normal SPList object from the SharePoint object model to work with the external list and it’s external data from the SQL server (in my case).
So if you haven’t already: Get on the SharePoint 2010 wagon and enjoy the ride!
Comments are closed
Archived comments
How to add new column to external list?
You could add a column in the datasource (in my case the SQL table) and then update your BCS configuration/entities to reflect that change.
Regards,
Tobias
the first link
1. SP 2010: Getting started with the Business Connectivity Services (BCS)
points to second link !
i m lost on how to start programming ...pl suggest
Hi Virali,
Unfortunately some of my links are broken from the migration. Use the search box and you'll find it.
Here's the link you're looking for: http://www.zimmergren.net/t...
Regards,
Tobias.
/ Get the external list
SPList list = web.Lists["Product 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"] = "Sample Description Wohoo";
item.Update();
I tried this but access is denied
Hi Achyut,
First things first; Do you have an External List called "Product List" with the two fields "Name" and "Description" ?
If you do, take a look at this article for clearing out one of the common reasons for getting "Access Denied": http://www.zimmergren.net/t...
Cheers,
Tobias.
check whether current user has permissions to add item in that list..do impersonation.
HI Tobias,
Can we do in reverse process i have a sharepoint list and i want a backup to my db how can i do this using timer job like bcs reverse process..
Thanks and regards
PradeepAnand
bpradeepanand@gmail.com
Hi PradeepAnand,
Please specify a bit more about what you want to achieve.
Cheers,
Tobias.
the previous article is not available... :(
Hi Anyone_10,
Which article is it that you're referring to?
Cheers,
Tobias.
Probably article 1 - getting started - the page seems to redirect to article 2...
Hi,
It has been fixed and the first article can now be reached properly from the link.
Regards,
Tobias.
How can we do search in External List programmatically?
Hi Gurmeetkaur2007,
By default you can enable search for your External Lists in SharePoint 2010. Refer to this link: http://social.technet.micro...
You can also create custom SPQueries to query an external list using the object model which is pretty straight forward, which I've mentioned above in the article.
Regards,
Tobias.
Dear Tobias,
Nice article. Could you please help me to get the ListItem ID from external list.
nice post. but I would like to know is there any threshold/cache limits exist while doing CRUD operations.
I managed to get BCS displaying external list inside sharepoint, but accessing it via console application does not work: if I query list via server side object model - list.GetItmes(q) fails with message: "Attempted to perform an unauthorized operation" - no errors in event log or sharepoint logs and no query ever hit database. But when I load list via client object model, call context.ExecuteQuery() fails with "The given key was not present in the dictionary" BUT sql query reached my database! (i used profiler to detect it). I use SharePoint foundation
How to pull external list data into .NET GridView using a SPDataSource? I have created an BCS connection and populated a External List of that SQL table in SharePoint. What I am now trying to accomplish is to create a GridView of the data using SPDataSource, but I continue getting errors. I have posted the full details http://social.msdn.microsof... and also http://www.experts-exchange... but I have not received any responses yet.
Is it possible to visualize external list data in a GridView? I sure it must. It looks as if the failure revolves around the <asp:parameter name="WebURL" defaultvalue="{rootsitecollection}"/> but I don't know how to fix it. Do you have any suggestions?
Much thanks.
Hi i have create a BCS list in which select all is not working. any IDEA??
ttt
Hi Tobias,
Nice post. I am able to add a column but while updating a value I am not able to fetch the ListItem by List.getItemById(someId) as the IDs generated by BCS columns are not integers. Any help would do.
Thanks in advance.
Hi Kushal,
You should be able to perform a query (SPQuery) and fetch the data you need that way. Just make sure that the field names are correct in the query.
cheers,
Tobias.
Hello, I want to know How to add a lookup field that use this external list ?