0

I have a sharepoint 2007 and in a document library eventhandler: in ItemUpdating, i would like to be able to retrieve the value by id's from lookupfield. So with other words i have to get the reference to a list to which the lookupfield is bound, to be able to get the values from there.

Any idea how to do that?

I have this:

SPField field = properties.ListItem..GetFieldByInternalName("InternalNameField");

I get the field, but how can I get the reference to a list to which it is bound? I couldn't find that this object has any properties like that.

2 Answers 2

1

You must cast the field to a SPFieldLookup. This object has members like

  • LookupList,

  • LookupField and

  • LookupWebId

    that will help you identify the correct list and field

See the MSDN documentation here

5
  • But if the field is multilookup field?
    – Shkipper
    May 13, 2013 at 10:56
  • And how do i get exactly the correct fielname that is linked to it?
    – Shkipper
    May 13, 2013 at 10:56
  • It is the same class, and you asked how to get the list to which it is bound, and you do that by the LookupList attribute that returns the GUID of the list.. May 13, 2013 at 10:58
  • Ok, but after i've got the list, how do i know from ID what field i should get to get correct value for it? I mean there should be reference to the column that it is bound too ... can you tell me what it is please?
    – Shkipper
    May 13, 2013 at 11:07
  • If you read the link i refered.. You will see this three properties: LookupField, LookupList and LookupWebId. They should enable you to do all you want. May 13, 2013 at 11:09
1

Try this code:

SPFieldLookup spfl = yourListName.Fields["yourFieldName"] as SPFieldLookup;
Guid _Guid = new Guid(spfl.LookupList);
SPList list = yourWebSiteName.Lists[_Guid];
foreach (SPListItem item in list.Items)
{
     if (item != null)
     {
          Console.WriteLine(item.ID);
          Console.WriteLine(item.Title + "\n");
     }
}

Your Answer

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

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