SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a lookup field on a list (header) that looks up values on the same list. I am trying to read the value out of that lookup with this code:

using (SPSite site = new SPSite (mySite.ID))
  using (SPWeb web = site.AllWebs[myWeb.ID])
    SPList list = web.Lists[myListGuid];
    SPListItem header = list.Items.GetItemById(headerId);

    // many working references to the fields within header

    SPFieldLookupValue myValue = header["DisplayName"] as SPFieldLookupValue;
    string s = myValue.LookupValue;

I am getting the following ArgumentException: "Value does not fall within the expected range."

Anyone know what I'm doing wrong?

I have modified my code to this:

if (!header.Fields.ContainsField("DisplayName"))
  SPFieldLookupValue poFieldValue = new SPFieldLookupValue(header["DisplayName"].ToString());
  string s = poFieldValue.LookupValue;

And I still get this same ArgumentException.

Further edits:

So I have been able to access this lookup value in my Added/Updated event receivers, but then I cannot access it much later in a custom workflow activity. So when I access it in the event receiver, I have gone ahead and stuck the value in a hidden text field. Then I can access the text field from the custom workflow activity.

Oh, and then last bit of weirdness. I can't actually access the field.LookupId and field.LookupValue. But I can access header["DisplayName"].ToString() and I get 215;#1345566. So I am just parsing it manually. This is very bizarre.

share|improve this question
header["DisplayName"] otherwise if DisplayName is a variable where are you setting it? – RJ Cuthbertson Dec 9 '12 at 21:31
it was a typo. i'm passing in a string of the displayname (which also is the same as the internal name) – Nathan DeWitt Dec 9 '12 at 21:33

When I cast an SPFieldLookupValue I do:


and it works fine.

Also, you can quickly access the LookupId or LookupValue without instantiating another object:

int id = ((SPFieldLookUpValue)(listItem["ColumnName"])).LookupId;
string value = ((SPFieldLookUpValue)(listItem["ColumnName"])).LookupValue;

A "Value does not fall within the expected range." error would only occur if the column could not be found. So if you are still getting that error, verify that the variable you're passing in isn't being modified anywhere, or step through with a debugger to see what the value of the string you're passing into the index is.

share|improve this answer

Maybe this workaround can help:

SPFieldLookupValue poFieldValue = new SPFieldLookupValue(header[header.Fields.GetFieldByInternalName("DisplayName").Id].ToString())
share|improve this answer

This error can occur if you are not requesting the field in your CAML Query. For example:

                var query = new SPQuery
                                ViewAttributes = "Scope=\"RecursiveAll\"",
                                RowLimit = 100,
                                ViewFields =
                                    "<FieldRef Name='BaseName' /><FieldRef Name='FileRef' /><FieldRef Name='DisplayName' />",
share|improve this answer

The item was not found into the list, the item is checked-in so the user is not be able to get it, the element is not in the list because was removed from there.


share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.