当我包含查询时,GetListItems超时

时间:2012-07-11 18:56:02

标签: c# web-services sharepoint

我正在尝试使用CAML查询来调用Lists.GetListItems。当我不包含查询参数时,它工作正常,但是当我包含查询时,我收到超时错误。假设我的Web服务名为TestWebService,代码为:

    public static void GetListItemsTest()
    {
        try
        {
            string listName = "TestList";
            string[] fields = { "ID", "Title" };
            string queryStr =
                       "<GroupBy collapse='true'>" +
                          "<FieldRef Name='" + fields[1] + "' />" +
                       "</GroupBy>" +
                       "<OrderBy>" +
                          "<FieldRef Name='ID' Ascending='False' />" +
                       "</OrderBy>";
            XmlDocument xmlDoc = new System.Xml.XmlDocument();
            XmlElement query = xmlDoc.CreateElement("Query");
            XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
            query.InnerXml = queryStr;
            viewFields.InnerXml = "";
            TestWebService.Lists lists = new TestWebService.Lists();
            lists.Url = "http://wss/sites/TestWebService/_vti_bin/lists.asmx";
            lists.Credentials = System.Net.CredentialCache.DefaultCredentials;
            XmlNode responseNode = lists.GetListItems(listName, null, query, viewFields, "1000000", null, null);
            Console.WriteLine("ran successfully");

        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
        finally
        {
            Console.ReadLine();
        }

    }

GetListItems的调用挂起,然后抛出WebException,并显示消息“操作已超时”。如果我将query.InnerXml分配更改为:

query.InnerXml="";

然后GetListItems成功返回responseNode的值。所以它必须与CAML查询片段有关。

在进一步调查中,我发现GroupBy元素是问题的根源;在注释掉OrderBy元素后,查询超时,并且注释掉GroupBy元素,查询成功。我想知道GroupBy是否无法处理大型数据集,或者它处理得太慢;这个集合有大约20,000个元素,每个Title值通常不超过5或6个重复。 LINQ在这种情况下会更适用吗?或者有没有办法用直接CAML做到这一点?

1 个答案:

答案 0 :(得分:1)

在SharePoint 2010系统上有20,000个项目,您可能会遇到 SPQueryThrottledException 的风险。典型用户限制为5000,一些特权用户限制为查询返回的20,000个项目。

您是否在Title列上设置了索引?这可能会提高GroupBy的性能。否则我怀疑查询强制检索所有项目,然后对它们进行分组。尝试对它们进行分组,然后在页面中检索它们,而不是一次性检索它们。

我不知道它是完全相关的,但我只是在large lists上发布关于查询的博客。