Introduction

This article explains what the various options are that we need to avoid list throttling exceptions.

Once you understand that the throttle exists and can catch exceptions, you may want to override the throttle, using the object model. There are a couple of ways to do this.

There are the following 3 options for SPQueryThrottleOption:

The default list throttling limit in SharePoint 2013 is 5000.

To modify the default setting go to:

Central Administration > Manage Web Applications > General Settings > Resource Throttling > List View Threshold

If we change this option then it will have global affect and that is not the best way. So, we will determine what are the various options we need to set it from code.

Example: Consider an where list throshold error.

  1. try
  2. {
  3. using (SPSite site = new SPSite(SPContext.Current.Site.Url))
  4. {
  5. using (SPWeb web = site.OpenWeb())
  6. {
  7. SPList list = web.Lists.TryGetList("PersonalInfo");
  8. SPQuery query = new SPQuery();
  9. // Define columns to fetch
  10. query.ViewFields = "<FieldRef Name=\"Title\" /><FieldRef Name=\"Address\" />";
  11. // Force to fetch only the specified columns
  12. query.ViewFieldsOnly = true;
  13. query.Query = "<Where><Contains><FieldRef Name=\"Address\" /><Value Type=\"Text\">Pune</Value></Contains></Where>";
  14. //Define the maximum number of results for each page (like a SELECT TOP)
  15. query.RowLimit = 10;
  16. // Query for items
  17. SPListItemCollection items = list.GetItems(query);
  18. foreach (SPListItem item in items)
  19. {
  20. Console.WriteLine(item["Title"] + " : " + item["Address"]);
  21. }
  22. }
  23. }
  24. }
  25. catch (SPQueryThrottledException)
  26. {
  27. //Retrieving all items can trigger the throttle
  28. }

The preceding query will provide more results if the list is large. We will see the options for how to work on Throttling from the code.

  1. try
  2. {
  3. using (SPSite site = new SPSite(SPContext.Current.Site.Url))
  4. {
  5. using (SPWeb web = site.OpenWeb())
  6. {
  7. SPList list = web.Lists.TryGetList("PersonalInfo");
  8. SPQuery query = new SPQuery();
  9. query.QueryThrottleMode = SPQueryThrottleOption.Override;
  10. // Define columns to fetch
  11. query.ViewFields = "<FieldRef Name=\"Title\" /><FieldRef Name=\"Address\" />";
  12. // Force to fetch only the specified columns
  13. query.ViewFieldsOnly = true;
  14. query.Query = "<Where><Contains><FieldRef Name=\"Address\" /><Value Type=\"Text\">Pune</Value></Contains></Where>";
  15. //Define the maximum number of results for each page (like a SELECT TOP)
  16. query.RowLimit = 10;
  17. // Query for items
  18. SPListItemCollection items = list.GetItems(query);
  19. foreach (SPListItem item in items)
  20. {
  21. Console.WriteLine(item["Title"] + " : " + item["Address"]);
  22. }
  23. }
  24. }
  25. }
  26. catch (Exception ex)
  27. {
  28. }

Set the SPQuery.QueryThrottleMode property to SPQueryThrottleOption.Override to disable throttling for a specific query.

This is a good way to disable throttling.

Note: In order to avoid a list throttling exception, the preconditions for the "Object Model override" attribute must be "Yes" and the query should be executed under Super User.