Pregunta

I'm able to use a console application to connect to a SharePoint Online large list (more than 5,000 items) and retrieve the title. However, even though my CAML query has a row limit, I still get an error when attempting to retrieve list items: "The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator."

My CAML syntax is based on this article: Overcoming the List View Threshold in SharePoint CAML queries

How can I successfully retrieve list items?

string siteUrl = "https://test.sharepoint.com/sites/test";
    string clientId = "xxxxx-aaaa-aaaa-ffff-gggggggggg";
    string clientSecret = "98hg984g3948j49qj";
    using (var cc = new AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl, clientId, clientSecret))
    {
        Web oWeb = cc.Web;
        List oList = oWeb.Lists.GetByTitle("Test List");
        cc.Load(oList, p => p.Title);
        cc.ExecuteQuery();
        Console.WriteLine(oList.Title);

        CamlQuery camlQuery = new CamlQuery();
        string viewXML = string.Format(@"<Query><View><ViewFields><FieldRef Name='ID' /></ViewFields><RowLimit>10</RowLimit></View></Query>");
        camlQuery.ViewXml = viewXML;

        ListItemCollection collListItems = oList.GetItems(camlQuery);
        cc.Load(collListItems);
        cc.ExecuteQuery(); //error occurs here

        Console.WriteLine("Total Count: " + collListItems.Count);

        foreach (ListItem item in collListItems)
        {
            Console.Write("Description: " + item["Description"]);
            Console.Write("Special ID: " + item["Special ID"]);
        }
        Console.ReadLine();
    };
¿Fue útil?

Solución

Summarizing the "discussion" from the comments above:

The codeproject article referenced in the question is apparently using either an incorrect or outdated schema for the CAML.
When you set the ViewXml property of the CamlQuery, the top-level XML element should be the <View>. The <Query> is one of the optional child elements. SharePoint was effectively ignoring your <RowLimit> element (rather than perhaps throwing a helpful exception about XML schema), thus resulting in the List View Threshold exception.

Licenciado bajo: CC-BY-SA con atribución
scroll top