Freigeben über


Using the SharePoint 2010 Managed Client Object Model – Accessing Large Lists

This is a clipboard friendly version of example #15, Accessing Large Lists, from Using the SharePoint 2010 Managed Client Object Model.

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

Blog TOC

using System;
using System.Linq;
using Microsoft.SharePoint.Client;

class Program
{
static void Main()
{
ClientContext clientContext =
new ClientContext("https://intranet.contoso.com");

List list = clientContext.Web.Lists
.GetByTitle("Client API Test List");

// First, add 20 items to Client API Test List so that there are
// enough records to show paging.
ListItemCreationInformation itemCreateInfo =
new ListItemCreationInformation();
for (int i = 0; i < 20; i++)
{
ListItem listItem = list.AddItem(itemCreateInfo);
listItem["Title"] = String.Format("New Item #{0}", i);
listItem["Category"] = "Development";
listItem["Estimate"] = i;
listItem.Update();
}
clientContext.ExecuteQuery();

// This example shows paging through the list ten items at a time.
// In a real-world scenario, you would want to limit a page to
// 2000 items.
ListItemCollectionPosition itemPosition = null;
while (true)
{
CamlQuery camlQuery = new CamlQuery();
camlQuery.ListItemCollectionPosition = itemPosition;
camlQuery.ViewXml =
@"<View>
<ViewFields>
<FieldRef Name='Title'/>
<FieldRef Name='Category'/>
<FieldRef Name='Estimate'/>
</ViewFields>
<RowLimit>10</RowLimit>
</View>";
ListItemCollection listItems = list.GetItems(camlQuery);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
itemPosition = listItems.ListItemCollectionPosition;
foreach (ListItem listItem in listItems)
Console.WriteLine(" Item Title: {0}", listItem["Title"]);
if (itemPosition == null)
break;
Console.WriteLine(itemPosition.PagingInfo);
Console.WriteLine();
}
}
}

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

using System;
using System.Linq;

class Program
{
static void Main()
{
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");

// First, add 20 items to Client API Test List so that there are
// enough records to show paging.
Microsoft.SharePoint.Client.ListItemCreationInformation itemCreateInfo =
new Microsoft.SharePoint.Client.ListItemCreationInformation();
for (int i = 0; i < 20; i++)
{
Microsoft.SharePoint.Client.ListItem listItem = list.AddItem(itemCreateInfo);
listItem["Title"] = String.Format("New Item #{0}", i);
listItem["Category"] = "Development";
listItem["Estimate"] = i;
listItem.Update();
}
clientContext.ExecuteQuery();

// This example shows paging through the list ten items at a time.
// In a real-world scenario, you would want to limit a page to
// 2000 items.
Microsoft.SharePoint.Client.ListItemCollectionPosition itemPosition = null;
while (true)
{
Microsoft.SharePoint.Client.CamlQuery camlQuery = new Microsoft.SharePoint.Client.CamlQuery();
camlQuery.ListItemCollectionPosition = itemPosition;
camlQuery.ViewXml =
@"<View>
<ViewFields>
<FieldRef Name='Title'/>
<FieldRef Name='Category'/>
<FieldRef Name='Estimate'/>
</ViewFields>
<RowLimit>10</RowLimit>
</View>";
Microsoft.SharePoint.Client.ListItemCollection listItems = list.GetItems(camlQuery);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
itemPosition = listItems.ListItemCollectionPosition;
foreach (Microsoft.SharePoint.Client.ListItem listItem in listItems)
Console.WriteLine(" Item Title: {0}", listItem["Title"]);
if (itemPosition == null)
break;
Console.WriteLine(itemPosition.PagingInfo);
Console.WriteLine();
}
}
}