Showing posts with label SPField. Show all posts
Showing posts with label SPField. Show all posts

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.

Wednesday, June 27, 2012

list.Fields["Internal Name"] does not exist

Got a quick and dirty one for you.

A co-worker of mine came to me with a simple but weird issue.
This chunk of code wasn't working:

 string property = "DocumentSetDescription";  
 if (list.Fields.ContainsField(property))  
 {  
   if (list.Fields[property].Type == SPFieldType.Invalid)  
   {  
     //Code Here  
   }  
 }  

But it was throwing an error on the second 'if' statement basically saying the Field did not exist.

So you would think that if the first if statement was true then the second must be OK as well but it wasn't.

Come to find out it has to do with the way that the SPList.Fields Property looks up fields.

if you use .Fields.ContainsField(value) it looks it up by the internal name of the field (which were had) but if you use .Fields[value] it tries to look it up by either the GUID, DisplayName or the iIndex.

So if you try to look up a column by the Internal Name and it isn't the same as the Display Name then it will fail.

In order to avoid this we refined the if statement to be more like this:

 string property = "DocumentSetDescription";  
 if (list.Fields.ContainsField(property))  
 {  
   if (list.Fields.GetFieldByInternalName(property).Type == SPFieldType.Invalid)  
   {  
     //Code Here  
   }  
 }  

You learn something new every day...