SharePoint 2010 List Throtelling

SharePoint 2010 List Throtelling
2 votes, 5.00 avg. rating (97% score)

Introduction

List throtelling is the behaviour which allows to configure SharePoint list to prevent any operation from returning too many results at any time.
In SharePoint 2010 one of the improvements is list throttling. In this article we will see what are the various options we have to avoid list throtelling exceptions.

List Throtelling

The default list throtelling limit in SharePoint 2010 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 it will affect globally which is not the good way. So, we will check what are the various options we have to set from the code

ListThrotelling options

Consider an example where list throshold error will be coming

string query = @"<Where>
                            <And>
                                <BeginsWith>
                                    <FieldRef Name='Title' />
                                    <Value Type='Text'>A</Value>
                                </BeginsWith>
                            </And>
                 </Where>"

 SPQuery listQuery = new SPQuery();
 listQuery.Query = query;
 SPList list = SPContext.Current.Web.Lists["EmpList"];
 SPListItemCollection items = list.GetItems(listQuery);

The above query will give more results if the list is large. We will see the various options how to work on throtelling from the code.

SPList.EnableThrottling

Setting the SPList.EnableThrottling property to false will disable throttling for a particular list.

In the above example if we change the code like the following it won’t give the throtteling exception

SPList list = SPContext.Current.Web.Lists["EmpList"];
list.EnableThrottling = false;
SPListItemCollection items = list.GetItems(listQuery);

But, in practcal this option is not the good one to use as throttling is disabled for the list.

SPQuery.QueryThrottleMode

Setting the SPQuery.QueryThrottleMode property to SPQueryThrottleOption.Override to disable throttling for a particular query.
This is a good way to disable throttling.
Note: Inorder to avoid list throtteling exception, precondions should be ‘Object Model override’ attribute must be ‘Yes’ and query should be executed under super user

In the above example the code will be like

  SPQuery listQuery = new SPQuery();
  listQuery.Query = query;
  listQuery.QueryThrottleMode = SPQueryThrottleOption.Override;
  SPList list = SPContext.Current.Web.Lists["EmpList"];
  SPListItemCollection items = list.GetItems(listQuery);

ContentIterator

We should consider using ContentIterator class in the Microsoft.Office.Server.Utilities for accessing large lists with throttling enabled or if the list is likely to grow in future.

  SPQuery listQuery = new SPQuery();
  listQuery.Query = query;
  SPList list = SPContext.Current.Web.Lists["EmpList"];
  string iteratorName = "EmpList Iterator";
  ContentIterator ci = new ContentIterator(iteratorName);
  ci.ProcessListItems(list, listQuery, ItemProcessor, ErrorProcessor);

  private static bool ErrorProcessor(SPListItem item, Exception e)
  {
	//logic here to handle exceptions thrown while processing a list.
        // Do not rethrow exception, keep iterating
	return true;
  }

   private static void ItemProcessor(SPListItem item)
   {
      // Put the code here to process a list item

   }

Conclusion

List throttling is not only for restriction on retrieval of data from the list. But, also it is the restriction on data that is affected. For example if our single action deletes 5000 items of the list, then also throtelling exception will occur.
So throtelling is determined by the number of rows affected in content database.

References

ContentIterator Class

April 29, 2012 В· Adi В· No Comments
Tags: , , , ,  В· Posted in: Lists, Sharepoint 2010

Leave a Reply

What is 10 + 7 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)