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:
- Default: The normal behavior where all users who are not web server box administrators will be subject to the List View Threshold, including users with "Full Read" or "Full Control" permissions. This is the default behavior unless otherwise specified.
- Override: If the user has “Full Control” or "Full Read" permissions, the List View Threshold for Auditors and Administrators will apply to this SPQuery and List View Lookup Threshold will not be applied. For more info on what the List View Lookup Threshold does, read this article.
- Strict: List View threshold will apply for everyone, including web server box administrators. You can use this option to ensure that your code does not cause server stress even if it is being run as the box administrator on one of the web servers, since box administrators are not subject to the thresholds so may inadvertently slow down the servers.
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.
- try
- {
- using (SPSite site = new SPSite(SPContext.Current.Site.Url))
- {
- using (SPWeb web = site.OpenWeb())
- {
- SPList list = web.Lists.TryGetList("PersonalInfo");
- SPQuery query = new SPQuery();
-
- query.ViewFields = "<FieldRef Name=\"Title\" /><FieldRef Name=\"Address\" />";
-
-
- query.ViewFieldsOnly = true;
- query.Query = "<Where><Contains><FieldRef Name=\"Address\" /><Value Type=\"Text\">Pune</Value></Contains></Where>";
-
- query.RowLimit = 10;
-
- SPListItemCollection items = list.GetItems(query);
- foreach (SPListItem item in items)
- {
- Console.WriteLine(item["Title"] + " : " + item["Address"]);
- }
- }
- }
- }
- catch (SPQueryThrottledException)
- {
-
- }
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.
- try
- {
- using (SPSite site = new SPSite(SPContext.Current.Site.Url))
- {
- using (SPWeb web = site.OpenWeb())
- {
- SPList list = web.Lists.TryGetList("PersonalInfo");
- SPQuery query = new SPQuery();
- query.QueryThrottleMode = SPQueryThrottleOption.Override;
-
-
- query.ViewFields = "<FieldRef Name=\"Title\" /><FieldRef Name=\"Address\" />";
-
-
- query.ViewFieldsOnly = true;
- query.Query = "<Where><Contains><FieldRef Name=\"Address\" /><Value Type=\"Text\">Pune</Value></Contains></Where>";
-
- query.RowLimit = 10;
-
- SPListItemCollection items = list.GetItems(query);
- foreach (SPListItem item in items)
- {
- Console.WriteLine(item["Title"] + " : " + item["Address"]);
- }
- }
- }
- }
- catch (Exception ex)
- {
- }
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.