使用客户端模型编程处理外部列表

原文名称:SP 2010: Programmatically work with External Lists (BCS) using the Client Object Model 

原文地址:http://www.zimmergren.net/archive/2010/01/21/sp-2010-programmatically-work-with-external-lists-bcs-using-the-client-object-model.aspx

介绍

这是这个系列的第三篇。

  • 在 SharePoint2010 中使用 BCS (Business Connectivity Services )
  • 在 SharePoint2010 中编程方式使用 BCS
  • 使用客户端模型编程处理外部列表
  • 在前几篇文章中,我讨论了如何简单地通过外部列表来处理数据,这篇文章,我将会讨论如何通过客户端对象模型来访问 BCS 中的数据。

    使用客户端对象模型读取外部列表数据

    在这个系列的前面文章中,我讨论了通过 SharePoint 2010 服务器端的 API 是如何简单地处理数据。

    现在,我们将会讨论通过客户端的对象模型来处理同样的问题。

    底层的数据

    我们仍然使用前一篇文章中的数据。

    我设计了一个 WinForm 应用程序,使用 Silverlight 对象模型或者 JavaScript 客户端对象模型。

    当点击名为 Get External Data 的按钮时,将会使用客户端对象模型来从外部列表中获取数据,然后显示在 DataGridView 中。

    代码

    // 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
    gvProducts.Rows.Add(
    product.ProductID,
    product.ProductName,
    product.ProductDescription
    );
    }

    参考:

  • Getting Started with the Client Object Model in SharePoint 2010
  • Getting Started with Business Connectivity Services in SharePoint 2010
  • 总结和下载

    在这个系列中,我通过三篇文章演示了如何创建一个 BCS 数据源,然后通过服务器端对象或者客户端对象从中获取数据。

    值得注意的是,这只是一个 Bete 版的演示。

    完整的 Visual Studio 2010 项目下载: [ Zimmergren.SP2010.ClientOM.BCS.zip ]

    原文地址:https://www.cnblogs.com/haogj/p/2033898.html