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.
header["DisplayName"]otherwise ifDisplayNameis a variable where are you setting it? – rjcup3 Dec 9 '12 at 21:31