0

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.

2
  • header["DisplayName"] otherwise if DisplayName is a variable where are you setting it? Dec 9, 2012 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) Dec 9, 2012 at 21:33

4 Answers 4

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.

2

Maybe this workaround can help:

SPFieldLookupValue poFieldValue = new SPFieldLookupValue(header[header.Fields.GetFieldByInternalName("DisplayName").Id].ToString())
1

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' />",
                            };
0

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.

?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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