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 an event reciever for item adding.

I'm using:

dropdwnvalue = web.Lists[properties.ListId].Fields["Names"].InternalName;

string selectedValue = properties.AfterProperties[dropdwnvalue].ToString();

In selectedValue I'm able to get the index of the selected dropdownbox. Is there any way to get the value "Bob" instead of the index?

share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

Check out this my blog post, it describes your situation.
Shortly, in before-receivers you always get a lookup id only (for lookup fields). If you want to get other item field values from lookup list, you must get item by this id and then get all required values.

share|improve this answer
    
Hi Kai, i tried from that example but, i m gettting the error that the item does not exits it must have been deleted by another user ..this is my code ----dropdwnvalue = web.Lists[properties.ListId].Fields["Names"].InternalName; var list = web.Lists["Demolist"]; if (int.TryParse(properties.AfterProperties[dropdwnvalue].ToString(), out id)) { var item = list.GetItemById(id); var categoryNo = item["Names"]; } I m getting the error at the line var item –  spStacker Jan 17 '12 at 18:35
    
If Names is a lookup field, from what list it gets data? –  Kai Jan 17 '12 at 18:53
    
Excellent Kai, i followed your example..and was able to get the name!..Thanks! –  spStacker Jan 17 '12 at 19:03
    
Thanks for following up Kai! –  Falak Mahmood Jan 17 '12 at 23:27
add comment

In general you can get it by using SPFieldLookupValue:

SPFieldLookupValue YourLookup = new (SPFieldLookupValue(Properties.AfterProperties.LisItem["Your lookup"] as string);

In your case the code would become:

dropdwnvalue = web.Lists[properties.ListId].Fields["Names"].InternalName;
String lookupFieldStringValue = Convert.ToString(properties.AfterProperties[dropdwnvalue]);
SPFieldLookupValue lookupFieldValue = new SPFieldLookupValue(lookupFieldStringValue);

Update

int lookedUpItemID = lookupFieldValue.LookupId;

If you want to use both LookupId and LookupValue then you can use SPFieldLookupValue constructor with two parameters

Something like this:

SPFieldLookupValue(1,"Test");
share|improve this answer
    
Hi..when i debugged it ..it is showing the lookupId=13..but the lookUpValue is "" empty.. –  spStacker Jan 17 '12 at 16:36
add comment

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.