1

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();
    };
  • Does the list also have a large number of columns? sometimes that also triggers the listviewthreshold error. Try including a <ViewFields> section in your query to limit which fields are being returned. – willman Jan 3 at 16:00
  • @willman Thanks for the suggestion. I added a ViewField above. I still get the same error. – Lauren Furness Jan 3 at 16:40
  • 1
    I know the article you linked to says different, but when setting the viewxml, I have always used the <View> element as the top level, with the <Query> as an optional child element as a peer with <ViewFields>, <RowLimit>, etc. Try just removing the outer <Query></Query> tags. – willman Jan 3 at 17:47
  • 1
    @willman you should make that an answer. The CAML is indeed malformed, which defaults to an "All Items" query. The Query element goes inside the View element. – Derek Gusoff Jan 3 at 18:25
  • summarized below, thanks. – willman Jan 3 at 18:59
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.