如何查询大型共享点列表

时间:2017-10-25 13:41:23

标签: sharepoint sharepoint-online csom caml

我有一个包含数万件物品的清单。

我尝试使用where子句限制查询,如下所示:

var list = Context.Web.Lists.GetByTitle(title);

string query = $@"<Query>
<Where>
    <Lt>
        <FieldRef Name='ID' /><Value Type='Integer'>100</Value>
    </Lt>
</Where>
<OrderBy><FieldRef Name='ID' Ascending='true' /></OrderBy>
</Query>";

var camlQuery = new CamlQuery();
camlQuery.ViewXml = query;

var items = list.GetItems(camlQuery);
Context.Load(items);
Context.ExecuteQuery();

但无论如何我得到了SPQueryThrottledException

  

禁止尝试操作,因为它超出了列表   查看管理员强制执行的阈值。

如何查询大型sp列表?

1 个答案:

答案 0 :(得分:0)

您需要使用ListItemCollectionPosition修改CAML查询并使用分页,并设置RowLimit值。

尝试并修改以下示例代码:

var list = Context.Web.Lists.GetByTitle(title);
ListItemCollectionPosition position = null;

try
{
    do
    {
        string query = $@"<View> 
            <ViewFields>
                <FieldRef Name='Title'/>
            </ViewFields>       
            <Query>
                <Where>
                    <Lt>
                        <FieldRef Name='ID' /><Value Type='Integer'>100</Value>
                    </Lt>
                </Where>
            <OrderBy><FieldRef Name='ID' /></OrderBy>
            </Query><RowLimit>100</RowLimit></View>";

        var camlQuery = new CamlQuery();
        camlQuery.ViewXml = query;


        var items = list.GetItems(camlQuery);
        Context.Load(items);
        Context.ExecuteQuery();
        position = items.ListItemCollectionPosition;

        foreach (ListItem listItem in items)
            Console.WriteLine("Title: {0}", listItem["Title"]);
    }
    while (position != null);

}
catch(Exception ex)
{

}

根据评论更新了CAML查询:

string query = $@"<View>                            
<Query>
    <Where>
        <And>
            <Geq>
                <FieldRef Name='ID' /><Value Type='Integer'>1</Value>
            </Geq>
            <Leq>
                <FieldRef Name='ID' /><Value Type='Integer'>1000</Value>
            </Leq>                                        
        </And>
    </Where>
<OrderBy><FieldRef Name='Created' /></OrderBy>
</Query><RowLimit>100</RowLimit></View>";