Wednesday, July 4, 2012

SharePoint 2010: Value does not fall within the expected range

Today I was working on a very annoying issue related to some custom code we are using to look-up information from a list.


All of a sudden when logged in as a non-Site Collection Administrator (Site Owner) our code was erroring out on this line:
 SPFieldUserValueCollection siteContacts = new SPFieldUserValueCollection(web, item["Site Contacts"].ToString());  


The error was basically saying that the 'Site Contacts' field did not exist in the list even tough it did.


Come to find out that this issue was not related to the list at all but rather the way that SharePoint gets list items. It does it in a CAML Query type of way thus the list item threshold of 8 columns applies and this column was magic number 9.


So because the list item threshold does not apply to Site Collection Admins the look-up worked fine for them but not for anyone else.


Lesson learned, increase the List View Threshold, perform a specific query on the list using SPQuery and specify less than 8 ViewFields something like this or override the limit like this:
 query.QueryThrottleMode = SPQueryThrottleOption.Override  


Or, in our case, we took a different (3rd) approach.


You can disable the throttling on a list by list basis so we opted for this PowerShell script:
 $web = Get-SPWeb http://your.site.com  
 $list = $web.Lists["My List"]   
 $list.EnableThrottling = $false  
 $list.Update()  



We knew this 'special' list is not going to get out of hand and we didn't want to change the threshold for the whole web application.

1 comment: