使用客户端模型编程处理外部列表
原文名称:SP 2010: Programmatically work with External Lists (BCS) using the Client Object Model
介绍
这是这个系列的第三篇。
在前几篇文章中,我讨论了如何简单地通过外部列表来处理数据,这篇文章,我将会讨论如何通过客户端对象模型来访问 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
);
}
参考:
总结和下载
在这个系列中,我通过三篇文章演示了如何创建一个 BCS 数据源,然后通过服务器端对象或者客户端对象从中获取数据。
值得注意的是,这只是一个 Bete 版的演示。
完整的 Visual Studio 2010 项目下载: [ Zimmergren.SP2010.ClientOM.BCS.zip ]
分类:
SharePoint
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?