0

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?

2

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.

3
  • 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? – Sergei Sergeev 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
1

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");
1
  • Hi..when i debugged it ..it is showing the lookupId=13..but the lookUpValue is "" empty.. – spStacker Jan 17 '12 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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