SharePoint Foundation 2010 托管客户端对象模型概述(五) --使用 CAML 查询列表
2012-02-24 14:05
381 查看
以下示例演示了如何使用 CAML 查询在上例中创建的列表。该示例将输出我们的测试列表中的 Development 项。 C# 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["Title"], item => item["Category"], item => item["Estimate"])); clientContext.ExecuteQuery(); foreach (ListItem listItem in listItems) { Console.WriteLine("Title: {0}", listItem["Title"]); Console.WriteLine("Category: {0}", listItem["Category"]); Console.WriteLine("Estimate: {0}", listItem["Estimate"]); Console.WriteLine(); } } } 该示例将生成以下输出。 Title: Develop proof-of-concept. Category: Development Estimate: 42 Title: Develop user interface. Category: Development Estimate: 18 您可能已注意到,在该示例中指定的 lambda 表达式与修整结果集一节所提供的示例中的 lambda 表达式之间的区别。必须使用 Include 扩展方法来指定要为所加载的集合中的每个项加载的属性。Lambda 表达式的 items 参数属于 ListItemCollection 类型,该类型当然不包含允许我们指定为集合中的项加载哪些属性的索引属性。应调用 Include 扩展方法,它允许我们指定要加载该子集合的哪些参数。Include 扩展方法中 lambda 表达式的参数的类型与集合项的类型相同。因此,您可以指定要为集合中的每个项加载的属性。 再次说明,完全没有必要了解 lambda 表达式的这种用法的确切语义,只需记住以下两点编码习惯即可: 如果要求客户端对象模型加载客户端对象(而不是客户端对象集合)的某些属性,则应在您直接添加到 Load 方法的 lambda 表达式中指定这些属性。 C# clientContext.Load(site, s => s.Title, s => s.Description); 如果要求客户端对象模型加载客户端对象集合中每个项目的特定属性,应使用 Include 扩展方法,并将指定所需属性的 lambda 表达式传递给 Include 方法。 C# clientContext.Load( listItems, items => items.Include( item => item["Title"], item => item["Category"], item => item["Estimate"]));
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
相关文章推荐
- SharePoint Foundation 2010 托管客户端对象模型概述(二) --使用对象标识
- SharePoint Foundation 2010 托管客户端对象模型概述(九) --访问大型列表
- SharePoint Foundation 2010 托管客户端对象模型概述 --创建 Windows 控制台托管的客户端对象模型应用程序
- SharePoint Foundation 2010 托管客户端对象模型概述(三) --修整结果集
- SharePoint Foundation 2010 托管客户端对象模型概述(七) --更新客户端对象\删除客户对象
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第9章节--客户端对象模型和REST APIs概览 托管代码(.NET)
- SharePoint服务器端对象模型 之 使用CAML进行数据查询(Part 2)
- 使用客户端对象模型读取SharePoint列表数据
- 在 SharePoint Foundation 2010 中使用事件接收器
- 使用客户端对象模型读取SharePoint列表数据
- [SharePoint Designer技巧-1]使用DataView与Form Web Part实现列表的关键字查询
- SharePoint服务器端对象模型 之 使用CAML进展数据查询
- SharePoint服务器端对象模型 之 使用CAML进行数据查询
- SP2010开发和VS2010专家"食谱"--第七章节--使用客户端对象模型
- SharePoint服务器端对象模型 之 使用CAML进行数据查询(Part 3)
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第9章节--客户端对象模型和REST APIs概览 总结
- 【半原创】SharePoint CAML 查询生成器[SharePoint 2010 CAML Query Builder]
- SharePoint on Windows Server 2008 R2 - "客户端不支持使用windows资源管理器打开此列表”
- 使用SharePoint Client OM来查询列表的注意事项(2)
- [SharePoint Designer技巧-1]使用DataView与Form Web Part实现列表的关键字查询--转载