Freigeben über


Using the SharePoint 2010 Managed Client Object Model – Using CAML to Query a List

This is a clipboard friendly version of example #6, Using CAML to Query a List, from Using the SharePoint 2010 Managed Client Object Model.

This blog is inactive.
New blog: EricWhite.com/blog

Blog TOC

using System;
using Microsoft.SharePoint.Client;

class Program
{
static void Main(string[] args)
{
ClientContext clientContext =
new ClientContext("https://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();
}
}
}

Following is the same example using fully qualified names for better discoverability.

using System;
using Microsoft.SharePoint.Client;

class Program
{
static void Main(string[] args)
{
Microsoft.SharePoint.Client.ClientContext clientContext =
new Microsoft.SharePoint.Client.ClientContext("https://intranet.contoso.com");
Microsoft.SharePoint.Client.List list = clientContext.Web.Lists
.GetByTitle("Client API Test List");
Microsoft.SharePoint.Client.CamlQuery camlQuery = new Microsoft.SharePoint.Client.CamlQuery();
camlQuery.ViewXml =
@"<View>
<Query>
<Where>
<Eq>
<FieldRef Name='Category'/>
<Value Type='Text'>Development</Value>
</Eq>
</Where>
</Query>
<RowLimit>100</RowLimit>
</View>";
Microsoft.SharePoint.Client.ListItemCollection listItems = list.GetItems(camlQuery);
clientContext.Load(
listItems,
items => items
.Include(
item => item["Title"],
item => item["Category"],
item => item["Estimate"]));
clientContext.ExecuteQuery();
foreach (Microsoft.SharePoint.Client.ListItem listItem in listItems)
{
Console.WriteLine("Title: {0}", listItem["Title"]);
Console.WriteLine("Category: {0}", listItem["Category"]);
Console.WriteLine("Estimate: {0}", listItem["Estimate"]);
Console.WriteLine();
}
}
}