SharePoint Online 中 BCS 的新增内容

从 2011 年 11 月的服务更新开始,Microsoft SharePoint Online 允许在 SharePoint Online 应用程序中使用 Microsoft Business Connectivity Services (BCS)。

上次修改时间: 2011年12月12日

适用范围: SharePoint Designer 2010 | SharePoint Foundation 2010 | SharePoint Server 2010

在 SharePoint Online 中提供

本文重点介绍在开发使用 BCS 的 SharePoint Online 应用程序时需要了解的信息。您必须是有经验的 BCS 开发人员,已经熟悉 BCS 和 SharePoint 的技术及体系结构。

有关 SharePoint Online 的最佳实践和设计模式的信息,请参阅 SharePoint Online:概述(针对开发人员)

SharePoint Online 中 BCS 开发的新增内容

2011 年 11 月的服务更新允许开发人员使用 SharePoint Online 创建 BCS 应用程序。

为 SharePoint Online 开发 BCS 应用程序有何不同

除少数例外情况外,为 SharePoint Online 开发 BCS 应用程序与为 SharePoint 的本地安装开发 BCS 应用程序非常类似。

通常情况下,在为 SharePoint Online 开发应用程序时,您需要访问沙盒解决方案框架,以便于通过使用一组 SharePoint 对象模型创建代码。不过,在编写代码以处理外部数据源时,您必须使用客户端对象模型 API。

使用客户端对象模型为 SharePoint Online 开发 BCS 解决方案

若要在 SharePoint Online 中以编程方式处理外部数据,您必须使用客户端对象模型。因为客户端对象模型 API 远程执行,所以它们不受适用于沙盒或服务器端解决方案的相同限制的约束。

对 BCS 使用客户端对象模型就像处理 SharePoint 的本机数据。客户端对象模型是基于 SPList 类构建的,用于通过 SharePoint Web 服务从客户端执行 CRUD 操作。以下示例演示一些有用的技术。

  • 获取列表中的项目

    以下代码演示如何包含正确的引用以使用客户端对象模型,然后从列表中检索项目。

    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"]);
        }
    }
    
  • 更新列表项

    使用客户端对象模型更新客户端对象相当简单。只需检索对象,更改属性,对更改的每个对象调用 Update 方法,然后调用 ExecuteQuery 方法即可。下面的示例修改 Client API Test List 中的项目,从而将所有开发项目的估计值增加 50%(一种常见操作)。

    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();
        }
    }
    
  • 删除客户端对象

    删除客户端对象同样简单。不过,从客户端对象集合中删除客户端对象时有一个非常重要的变化。无法循环访问该集合,从而删除对象。在删除第一个对象后,将立即导致客户端对象集合的迭代器出现故障。该迭代器可能引发异常,也可能安静地完成但不访问集合中的全部项目。必须使用 ToList 方法将集合具体化为 List<T>,然后循环访问该列表,从而删除客户端对象。

    下面的示例从 Client API Test List 中删除测试项目。它演示在循环访问集合之前使用 ToList 方法具体化集合。

    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();
        }
    }
    

有关使用客户端对象模型的详细信息,请参阅以下资源:

请参阅

其他资源

SharePoint Online 开发人员资源中心

SharePoint Online 中的 Business Connectivity Services 简介