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 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.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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

share|improve this answer
    
But if the field is multilookup field? –  Shkipper May 13 '13 at 10:56
    
And how do i get exactly the correct fielname that is linked to it? –  Shkipper May 13 '13 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.. –  Robert Lindgren May 13 '13 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 '13 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. –  Robert Lindgren May 13 '13 at 11:09

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");
     }
}
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.