What's New for BCS in SharePoint Online

Beginning with the service update in November 2011, Microsoft SharePoint Online provides the ability to use Microsoft Business Connectivity Services (BCS) in SharePoint Online applications.

Applies to: SharePoint Designer 2010 | SharePoint Foundation 2010 | SharePoint Server 2010

Available in SharePoint Online

This article highlights information that you need as you develop SharePoint Online applications that use BCS. You must be an experienced BCS developer who is familiar with the techniques and architecture of BCS and SharePoint.

For information on best practices and design patterns for SharePoint Online, see SharePoint Online: An Overview for Developers.

What's New for BCS Development in SharePoint Online

The service update in November 2011 enables developers to create BCS applications by using SharePoint Online.

What's Different about Developing BCS Applications for SharePoint Online

Developing BCS applications for SharePoint Online is very similar to developing them for on-premises installations of SharePoint, with a few exceptions.

Normally, when developing applications for SharePoint Online, you have access to the sandboxed solutions framework, allowing you to create code by using a subset of the SharePoint object model. However, when writing code to manipulate external data sources, you must use the client object model APIs

Developing BCS Solutions for SharePoint Online Using Client Object Model

To programmatically work with external data in SharePoint Online, you must use the client Object model. Because the client object model APIs execute remotely, they are not subject to the same restrictions that apply to sandboxed or server-side solutions.

Using the client object model for BCS is just like working with data that is native to SharePoint. The client object model builds on the SPList class to perform CRUD operations from the client through the SharePoint Web services. The following examples illustrate some useful techniques.

  • Getting Items in a List

    The following code demonstrates how to include the proper reference to work with the client object model and then retrieve items from a list.

    using System;
    using Microsoft.SharePoint.Client;
    
    class Program
    {
        static void Main()
        {
            ClientContext clientContext = new ClientContext("http://intranet.contoso.com");
            List list = clientContext.Web.Lists.GetByTitle("Customer Info");
            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml = "<View/>";
            ListItemCollection listItems = list.GetItems(camlQuery);
            clientContext.Load(list);clientContext.Load(listItems);
            clientContext.ExecuteQuery();
            foreach (ListItem listItem in listItems)
                Console.WriteLine("Id: {0} Title: {1}", listItem.Id, oListItem["Title"]);
        }
    }
    
  • Updating a List Item

    Updating client objects by using the client object model is fairly simple. You retrieve the objects, alter properties, call the Update method for each object that you change, and then call the ExecuteQuery method. The following example modifies items in the Client API Test List, increasing the estimate for all development items by 50 percent (a common operation).

    using System;
    using Microsoft.SharePoint.Client;
    
    class Program
    {
        static void Main(string[] args)
        {
            ClientContext clientContext = new ClientContext("http://intranet.contoso.com");
            List list = clientContext.Web.Lists.GetByTitle("Client API Test List");
            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml =
                @"<View>
                    <Query>
                      <Where>
                        <Eq>
                          <FieldRef Name='Category'/>
                          <Value Type='Text'>Development</Value>
                        </Eq>
                      </Where>
                    </Query>
                    <RowLimit>100</RowLimit>
                  </View>";
            ListItemCollection listItems = list.GetItems(camlQuery);
            clientContext.Load(
                 listItems,
                 items => items.Include(
                     item => item["Category"],
                     item => item["Estimate"]));
            clientContext.ExecuteQuery();
            foreach (ListItem listItem in listItems)
            {
                listItem["Estimate"] = (double)listItem["Estimate"] * 1.5;
                listItem.Update();
            }
            clientContext.ExecuteQuery();
        }
    }
    
  • Deleting Client Objects

    Deleting client objects is just as easy. However, there is one very important dynamic around deleting client objects from a client object collection. You cannot iterate through the collection, deleting objects. As soon as you delete the first object, it causes the iterator of the client object collection to malfunction. The iterator may throw an exception, or it may quietly finish but not visit all items in the collection. Instead, you must materialize the collection into a List<T> using the ToList method, and then iterate through that list, deleting the client objects.

    The following example deletes the test items from our Client API Test List. It shows using the ToList method to materialize the collection before you iterate through it.

    using System;
    using System.Linq;
    using System.Collections.Generic;
    using Microsoft.SharePoint.Client;
    
    class Program
    {
        static void Main(string[] args)
        {
            ClientContext clientContext = new ClientContext("http://intranet.contoso.com");
            List list = clientContext.Web.Lists.GetByTitle("Client API Test List");
            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml =
                @"<View>
                    <Query>
                      <Where>
                        <Eq>
                          <FieldRef Name='Category'/>
                          <Value Type='Text'>Test</Value>
                        </Eq>
                      </Where>
                    </Query>
                    <RowLimit>100</RowLimit>
                  </View>";
            ListItemCollection listItems = list.GetItems(camlQuery);
            clientContext.Load(
                 listItems,
                 items => items.Include(
                     item => item["Title"]));
            clientContext.ExecuteQuery();
            foreach (ListItem listItem in listItems.ToList())
                listItem.DeleteObject();
            clientContext.ExecuteQuery();
        }
    }
    

For more information on using the client object model, see the following resources:

See Also

Other Resources

SharePoint Online Developer Resource Center

Introduction to Business Connectivity Services in SharePoint Online