Monday, December 13, 2010

SharePoint 2010 Error: Accessing lookup field values using Elevated web generates exception “Value does not fall within the expected range” when the lookup fields exceeds the lookup threshould

I have a list which has 9 look columns. By default SharePoint doesn’t allow that more than 8 lookup columns (However you can modify the value). When I tried to access the list from SharePoint list view, I could access the list without any problem. I was even accessing the list from webpart code and it was working fine.


All of sudden I had found some piece of my code is not working. While I was trying to access the 9th lookup column value of the SharePoint List from webpart  code I had got the error “Value does not fall within the expected range”. After investigating the problem I had found the 9th field doesn’t exist in the list. After spending some time on the issue, the final summary is:

So, If your list’s lookup columns exceeds the Lookup column threshold and if you try to access the list lookup field value from code using elevated web, then you will get the error “Value does not fall within the expected range”. And interestingly the exception will be thrown for not all lookup fields rather the ‘n+1’ lookup fields whereas the n is the lookup field threshold value."

Problem At a glance

So here is how you can reproduce the issue:

  1. You have set the list view lookup threshold to N.
  2. Then you have a list MyList with more than N lookup fields.
  3. If you try to access the list from SharePoint UI, you can access the list.
  4. Even if you try to access the list from code with SharePoint object model using SPContext.Current.Web you can access all lookup field values.
  5. However, If you try to access the lookup field values using Elevated web (code under SPSecurity.RunWithElevated) you will get error for N+1 lookup fields.

Sample Code to regenerate the issue

The following code block I used to test the issue. To run the test I had set the lookup column limit to two from central admin. So the code can read first two field’s lookup value but get exception to read the third.

var webId = SPContext.Current.Web.ID;
var siteId = SPContext.Current.Site.ID;
() =>
    using (SPSite site = new SPSite(siteId))
        using (SPWeb web = site.OpenWeb(webId))
            var fields = new string[] {"LooupField1", "LookupField2", "LookupField3"};
            var lookupTestList = web.Lists["LookupTestList"];
            foreach (SPListItem spListItem in lookupTestList.Items)
                foreach (var f in fields)
                        //get excception here for reading lookup col 3
                        var value1 = spListItem[f];
                    catch (Exception exception)
                        Response.Write(string.Format("Error in reading field: {0}. Error: {1}", f, exception.Message));

The points to notice to regenerate the issue:

  • The exception is not thrown if I try the same code shown above in console application. However, the exception is thrown when I tried to run it webpart.
  • The exception is shown when I try to get the items by accessing List.Items. However the exception is not thrown when I get the item using any other means (like list.GetItemById etc)


  1. this post is a life saver. i was stuck with the problem for hours.

  2. realy life saver! thanks a lot...


Note: Only a member of this blog may post a comment.