Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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"))
{
  //
}
else
{
  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

2 Answers 2

When I cast an SPFieldLookupValue I do:

 (SPFieldLookUpValue)(listItem["ColumnName"])

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

Your Answer

 
discard

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.