冠军

思胜.NET 高级培训

导航

公告

统计

2011年5月1日 #

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

原文名称: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 ]

    posted @ 2011-05-01 21:01 冠军 阅读(265) 评论(1) 编辑

    在 SharePoint2010 中编程方式使用 BCS

    原文名称:SP 2010: Programmatically work with External Lists (BCS) in SharePoint 2010

    原文地址:http://www.zimmergren.net/archive/2010/01/19/sp-2010-programmatically-work-with-external-lists-bcs-in-sharepoint-2010.aspx

    上一篇文章:在 SharePoint2010 中使用 BCS (Business Connectivity Services )

    介绍

  • 在 SharePoint2010 中使用 BCS (Business Connectivity Services )
  • 在 SharePoint2010 中编程方式使用 BCS
  • 使用客户端模型编程处理外部列表
  • 在前一篇文章中,我介绍了在 SharePoint2010 中如何简单地配置和使用外部数据,在这篇文章中,我将会介绍如何利用 SharePoint2010 的对象模型来使用外部数据,真的很简单!

    通过 SharePoint 对象模型使用外部数据

    这个例子中的代码与通过 SharePoint 2007 或者 2010 中的其他列表来获取信息没有什么实质的不同。当然,我们不需要学习任何新的框架或者工具来使用外部数据,基本上,就像其他任何的 SPList 一样简单。

    获取外部数据

    在上一篇文章中, SQL Server 中已经存在了一张表:ProductList。表中的数据如下所示:

    从外部列表中获取数据并显示的控制台程序如下所示:

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

    显示效果如下:

    这就是通过 BCS 从 SQL Server 获得数据需要的工作!

    保存到外部列表

    太简单了,类似下面这样。

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

    在 SharePoint 中运行程序,将会创建一个 SPListItem对象,当执行Update() 之后,将会通过数据连接,保存到 SQL Server 中。

    数据库中的保存的结果如下:

    我们在运行一个 Beta 产品!

    就像你想到的,在 SharePoint 2010 中有许多很酷的事情,BCS 就是其中之一,这篇文章讨论了很基础的通过列表 API 来获取和保存数据的问题。

    在写这篇文章的时候,没有任何性能和服务器方面扩展的考虑。当我考虑的时候,我会继续进行说明。

    总结

    像你看到的,使用 SharePoint API 不是很难,你可以通过通常的 SPList 对象来使用外部数据。

    所以,如果需要的话,使用 SharePoint 2010 吧!

    posted @ 2011-05-01 14:48 冠军 阅读(992) 评论(1) 编辑

    在 SharePoint2010 中使用 BCS (Business Connectivity Services )

    原文标题:SP 2010: Getting started with Business Connectivity Services (BCS) in SharePoint 2010 

    原文地址:http://www.zimmergren.net/archive/2010/01/18/sp-2010-getting-started-with-business-connectivity-services-bcs-in-sharepoint-2010.aspx

    介绍

    这是一个 BCS 系列中的第一篇。

    1. 在 SharePoint2010 中使用 BCS (Business Connectivity Services )
    2. 在 SharePoint2010 中编程方式使用 BCS
    3. 使用客户端模型编程处理外部列表

    SharePoint 2010 中的BCS 是 MOSS 2007 中 Business Data Catalog 的新版本,通过 BCS,可以连接和使用外部数据。

    在这个系列的第一篇文章中,我不会去介绍 BCS 的基本知识,MSDN 中介绍的已经很详细:Business Connectivity Services 概述 (SharePoint Server 2010),我会直接带你完成一次设置 BCS 连接到外部的数据源,然后,通过 SharePoint 中的列表来使用这些信息,不需要你具备任何连接到数据库的知识。

    关于 BCS: Business Connectivity Services Poster

    BCS 团队的博客:http://blogs.msdn.com/bcs/

    示例 Sql 数据库

    先介绍一下我的示例数据库,简单地在你的 SQL Server 中创建一个数据库,然后填充一些示例的数据,在这个练习中,数据库的名字叫做 Zimmergren_DB:

    在这个数据库中,我创建了一个名为 ProductList 的表,用来表示一些产品,就像下面这样。

    然后,填充了一些示例的数据,以便我们在后面通过 SharePoint 可以看到这些数据。

    好啦!我们已经在 SQL Server 中有了示例的数据,非常的简单,太棒了,让我使用这些有趣的数据继续吧!

    创建外部数据类型

    为了更加有效和简单地创建一个 BCS 连接,可以通过 SharePoint Designer 2010 来完成。听我说,你可以不使用复杂的 ADF 文件和类似的东西,我们使用 SharePoint Designer2010 来完成它。

    为了完成这个任务,我们需要先创建一个新的外部内容类型( External Content Type)。

    下面我们一步一步地创建外部内容类型:

    使用 SharePoint Designer2010 打开你的站点。

    在左边的导航上,选择 External Content Types 。

    点击创建一个新的外部内容类型 (External Content Type) ,如下图所示:

    点击连接:"Click here to discover external data sources and define operations“:

    点击创建新连接( Add Connection ):

    在数据源类型 ( Data Source Type ) 中,选择 SQL Server,

    输入详细的 SQL Server 连接信息:

    建立连接之后,数据源浏览器 (Data Source Explorer ) 将会显示你的的数据,选择你希望使用的表,然后,右击,选择创建所有的操作 (Create All Operations):

    现在,你会看到一个向导。

    点击继续 Next,然后,看到参数页 Parameters

    选择标识列,在这个练习中,我的 ProductID 就是。

    点击完成 Finish

    你会看到一个外部内容类型的可操作列表。

    就这样啦,非常简单,下面我们创建一个外部列表,使用我们外部内容类型来填充它。

    创建一个外部列表

    在 SharePoint2010 中有几种不同的方法可以创建外部类别,我们使用浏览器来完成它,你可以看到是多么的简单!

    打开你的站点,在站点操作 Site Actions 中,选择其他操作 More Options

    选择外部列表 External List 模板,然后创建 Create

    输入列表的名称,例如:Product List

    你将会看到一个外部内容类型的输入框,点击旁边的浏览按钮:

    太棒了!你只需要简单地为列表选择一个数据源,这意味着,你需要选择刚刚创建的称为 Zimmergren_DB d的数据源,你的列表将会自动连接到 SQL 数据库,但是,还需要处理一下列表的观感。

    选择数据源,然后确定

    然后,点击创建 Create

    看到了吗?现在你就可以通过一个普通的SharePoint List 来使用外部的数据了。

    现在,你可以创建一个新的条目,更新现有的条目,删除条目,通过 SharePoint 2010 列表完成 CRUD!

    创建一个产品

    让我先创建一个新的产品 Awesome Product 1.0,

    然后,到数据库中看一看,数据不是保存在 SharePoint 中,它已经被保存到数据库中了。

    Summary

    通过一些简单的点击操作,你已经创建了一个外部数据连接,就这么简单!

    当然,还有许多的事情需要考虑,你可能不希望自动生成 CRUD ,而是更加细粒度的通过权限来限制具体的操作。

    这个示例像你展示了可以如此简单地通过 SharePoint 2001 的 Business Connectivity Service (BCS ) 来使用外部的数据!

    posted @ 2011-05-01 13:22 冠军 阅读(1075) 评论(2) 编辑