19JAN 2010

SP 2010: Programmatically work with External Lists (BCS) in SharePoint 2010


Posted by Tobias Zimmergren

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

Introduction

Article 2 in the small BCS-series:

1. SP 2010: Getting started with the Business Connectivity Services (BCS)
2. SP 2010: Programmatically work with External Lists (BCS) in SharePoint 2010
3. 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 really 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:

image

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 =
“<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):

image

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…

// 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. 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:
image

We’re running a Beta-product!

As you can imagine, there’s a ton of new cool things to work with in SharePoint 2010 – where the BCS is one. This article discuss the very basics of how you can retrieve information from these lists using the normal API-approach.

At the time of this writing (during Public Beta) there isn’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 be 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!

  • Venkatbabu Mogili

    How to add new column to external list?

    • http://www.zimmergren.net/ Tobias Zimmergren

      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

  • Achyut neupane

    / 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

    • http://www.zimmergren.net/ Tobias Zimmergren

      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/technical/access-denied-by-business-data-connectivity-solution

      Cheers,
      Tobias.

    • revanth

      check whether current user has permissions to add item in that list..do impersonation.

  • Interstate Removalists

    I did give useful information from the heart If you post more than please inform me by clicking this link Interstate Removalists

  • http://www.practiceleague.com/compliance-risk-management-solutions-system.html Compliance risk management

    Thanks for such a nice post. These tips are really helpful. Again thanks for sharing your knowledge with us. I found a lot of useful tips from this post. Keep blogging.

  • Bpradeepanand

    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

    • http://www.zimmergren.net/ Tobias Zimmergren

      Hi PradeepAnand,

      Please specify a bit more about what you want to achieve.

      Cheers,
      Tobias.

  • http://www.401kgps.com/contribution-limits.aspx 401k Limits

    It is really a good and unique blog. Very Good tutorial for SQL and web designing. I am looking to invest aprt of 401k retirement plan in SQL and web design industry, so this is very useful for me.

  • Escort Service004
  • Anyone_10

    the previous article is not available… :(

    • http://www.zimmergren.net/ Tobias Zimmergren

      Hi Anyone_10,

      Which article is it that you’re referring to?

      Cheers,
      Tobias.

      • G Charley

        Probably article 1 – getting started – the page seems to redirect to article 2…

        • http://www.zimmergren.net/ Tobias Zimmergren

          Hi,
          It has been fixed and the first article can now be reached properly from the link.
          Regards,
          Tobias.

  • http://www.cancercareweb.com/ Cancer Treatment

    I am really happy to read this post and it contains many useful facts. I am happy to find your distinguished way of writing the post.

  • Pingback: SP 2010: Customizing the forms for External Lists (BCS) in SharePoint 2010 by using Custom Field Controls and jQuery | Tobias Zimmergren's thoughts on development

  • http://www.bariatricspecials.com/high-low-hospital-beds.html low hospital beds

    I make sure to bookmark it and return to read more of your useful information. Thanks for the post. I will definitely comeback

  • http://casinospiele2000.com/ casino spielen

    I would like to thank you for the efforts you have made while writing this post. I am hoping for the best work of the same from you in future.

  • Gurmeetkaur2007

    How can we do search in External List programmatically?

  • Gowri Sankar

    nice post. but I would like to know is there any threshold/cache limits exist while doing CRUD operations.

  • Serge Laman

    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

  • Alex Dove

    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.microsoft.com/Forums/en-US/sharepointdevelopmentprevious/thread/c8690684-e4ff-4c8c-9f12-47624cb3ee0b and also http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/Server/MS-SharePoint/Q_27973318.html#a38775341 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 but I don’t know how to fix it. Do you have any suggestions?

    Much thanks.

  • sujan singh

    Hi i have create a BCS list in which select all is not working. any IDEA??

  • tt

    ttt