0

I have an SPListItem object from a list based on a custom content type. Many of the fields in that content type are lookup types that point to other lists. When I get back the SPListItem object for a list based on that content type, the value it contains for any lookup field is the ID from the lookup list, not the text that is displayed in the dropdown in the UI. If I try to create an SPFieldLookupValue to get the text, it just doesn't work because the intent of object seems to be the opposite, that you have the text and want to get the ID (MSDN Page). Here's the non-working function I have:

    public static bool DocumentIsExpired(SPListItem item)
    {
        if (item.Properties["Expiry Status"] != null && !string.IsNullOrEmpty(item.Properties["Expiry Status"].ToString()))
        {
            var fieldVal = new SPFieldLookupValue(item.Properties["Expiry Status"].ToString());

            if (string.Equals(fieldVal.LookupValue, "EXPIRED", StringComparison.OrdinalIgnoreCase))
            {
                return true;
            }
            if (string.Equals(fieldVal.LookupValue, "UNEXPIRED", StringComparison.OrdinalIgnoreCase))
            {
                return false;
            }
        }
    }

Do I just need to fetch the list and look it up myself? Or is there something wrong with how the content type was created or configured that I am seeing the ID and not the text in the SPListItem?

2
  • I'm not sure why that wouldn't work - and I'll keep thinking about that - but I note that if someone sets another value (Not EXPIRED and not UNEXPIRED), your code will not return true or false. In fact, several code paths don't return a value.
    – Andy Burns
    Commented Dec 10, 2010 at 15:43
  • Where is this code fired ? If it is in an Event Receiver, depending if sync or async, you won't have the good values in your property bag. Commented Dec 10, 2010 at 15:59

1 Answer 1

1

If item is SPListItem and Expiry Status is display name of lookup column then you should use:

var fieldVal = new SPFieldLookupValue(item["Expiry Status"].ToString());

You are using item.Properties so you are not referencing item columns but property bag of SPListItem and by default its empty.

Your Answer

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