Question

I want to disable list throttling through code so i have written the following code

SPList lstBookings = web.Lists.TryGetList("Bookings");
                    lstBookings.EnableThrottling = false;

But does this requires the code to be running under elevated privileges, because if i login as a non admin user it gives me an error cannot login.

Anyone any ideas?

Thanks.

Was it helpful?

Solution

EnableThrottling seems to require Farm Administrator privileges. Take a look at the following code:

public bool EnableThrottling
{
    get { ... }
    set
    {
        bool nothrottlevalue = !value;
        this.SetListNoThrottle(nothrottlevalue);
    }
} 

internal void SetListNoThrottle(bool nothrottlevalue)
{
    SPSite site = this.ParentWeb.Site;
    if (!site.WebApplication.Farm.CurrentUserIsAdministrator(true))
        SPGlobal.HandleUnauthorizedAccessException(new UnauthorizedAccessException());
    else
        { ... }
}

The system account, in turn, has Site Collection Administrator privilege. The SPSecurity.RunWithElevatedPrivileges allows to run code under the Application Pool identity, which has same Site Collection Administrator privileges on all site collections.

OTHER TIPS

Run this piece of code as the Application Pool identity user:

SPSecurity.RunWithElevatedPriveleges( () => {
     using (var site = new SPSite(SPContext.Current.Site.ID))
                {
                    var web = site.RootWeb;
                    var lstBookings = web.Lists.TryGetList("Bookings");
                    lstBookings.EnableThrottling = false;
                    lstBookings.Update();
                }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top