Override List Throttling in SharePoint 2010

By Jatin Prajapati

In this FAQ we will see how to override the List throttling setting using C# and Object Model.

As, SharePoint 2010 provides list throttling to trim out the result set from getting excessive results. But in some cases you need to avoid this and want more results. SharePoint 2010 provided this functionality and allowed to override the list throttling while using SPQuery object to get list items. Following code snippet show you how to override the list throttling and get the list items which out gettting the SPQueryThrottledException. To test this code, be sure that your logged in user must not be site administrator or site owner.

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Text;

namespace SP2010Example
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                SP2010ListThrottlingProgramming sp2010List = new SP2010ListThrottlingProgramming();
                 
                sp2010List.OverrideThrottlingAndSearch();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            Console.Read();
        }
    }

    class SP2010ListThrottlingProgramming
    {
       public void OverrideThrottlingAndSearch()
        {
            using (SPSite site = new SPSite("http://win2008"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    try
                    {
                        SPList list = web.Lists["Test List"];
                        SPQuery query = new SPQuery();
                        query.QueryThrottleMode = SPQueryThrottleOption.Override;
                        query.Query = "<OrderBy><FieldRef Name=\"Title\" /></OrderBy>";
                        SPListItemCollection itemCollection = list.GetItems(query);
                        Console.WriteLine("Items found: {0}", itemCollection.Count);
                    }
                    catch (SPQueryThrottledException ex)
                     {
                         Console.WriteLine(ex.ToString());
                     }
                 }
             }
         }
    }
    
}

Override List Throttling in SharePoint 2010  (1086 Views)