Ask a questionAsk a question
 

AnswerItemAdding() Getting Values from DropDown List on Edit Form

  • Wednesday, February 06, 2008 12:29 PMGR101 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi,
    I'm using an ItemAdding Event on a List, ListA.  On the Edit Form there is a drop down lookup field that does a lookup on another list ListB.

     

    I've written code that takes the value from the lookup dropdown and gets more information from ListB (ie if UserId is selected then I pull through Telephone and Surname as well) from ListB and populate ListA with it.

     

    Previously this was sitting in an ItemAdded() event which means the value from the drop down has already been populated into the field so I could use ListItem["FieldName"] to get what was selected on the edit page's dropdown list.
    But in the ItemAdding() event this value has not been populated yet so I dont know how to get it.

    Does anyone know how I can get it?

    Thanks

     

Answers

  • Thursday, February 07, 2008 12:31 AMIshai Sagi[MOSS MVP]MVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    In ItemAdding you have to use AfterProperties, and not connect to the list item. See my blog post http://www.sharepoint-tips.com/2006/08/bad-news-synchronous-list-events-bug.html

     

  • Thursday, February 07, 2008 10:11 AMIshai Sagi[MOSS MVP]MVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    AfterProperties require the field's internal name, not the display name like you use in ListItem[name].

     

    You should do something like this in itemadding:

     

    Code Snippet
    using (SPWeb web = properties.OpenWeb())               
    {

    //get the current list
    SPList list = web.Lists[properties.ListId];

    SPField field = list.Fields[LookupValueA];

    object fieldVal = properties.AfterProperties[field.InternalName];

    SPFieldLookupValue fieldVal = new SPFieldLookupValue(properties.AfterProperties[field.InternalName].toString());

    }

     

     

     


All Replies

  • Thursday, February 07, 2008 12:31 AMIshai Sagi[MOSS MVP]MVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    In ItemAdding you have to use AfterProperties, and not connect to the list item. See my blog post http://www.sharepoint-tips.com/2006/08/bad-news-synchronous-list-events-bug.html

     

  • Thursday, February 07, 2008 10:02 AMGR101 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I have that link bookmarked and most paths I follow lead to it.

    Thank you for that posting it is very useful.

     

    However I need to take a value that is selected in the EditForm (a lookup field on another list) and use that value to to a SPQuery on the list it's linked to. 

    That value is not available in the AfterProperties. 

     

    In the ItemAdded() event the value has been populated in the list item so listItem["LookupValueA"] exists.
    But in the ItemAdding() event the value has not been populated yet so properties.AfterProperties["LookupValueA"] does not exist.

     

    Do you know how I could get the value of LookUpValueA?

  • Thursday, February 07, 2008 10:11 AMIshai Sagi[MOSS MVP]MVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    AfterProperties require the field's internal name, not the display name like you use in ListItem[name].

     

    You should do something like this in itemadding:

     

    Code Snippet
    using (SPWeb web = properties.OpenWeb())               
    {

    //get the current list
    SPList list = web.Lists[properties.ListId];

    SPField field = list.Fields[LookupValueA];

    object fieldVal = properties.AfterProperties[field.InternalName];

    SPFieldLookupValue fieldVal = new SPFieldLookupValue(properties.AfterProperties[field.InternalName].toString());

    }

     

     

     


  • Friday, February 08, 2008 9:29 AMGR101 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     

    My apologies..

    There were multiple content types so the field I was looking for was actually not in the content type that was firing the event. Have rectified the code accordingly.

     

    Ishai you da man!

     

    I've got another funny though.  When I create an item and set a value it is blank (even to the user that added the detail) until the item is checked in.  Is has this happened to you in when using AfterProperties (I'm doing other stuff in the EventHandler so wondering if it's the other code that's messing it around)?

    Thanks

  • Monday, February 11, 2008 10:28 AMGR101 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     

    Got it running.
    There was code elsewhere that was causing an error to be thrown and thus the AfterProperties were not being set.  The error was being thrown after the event handler had fired thus it was not obvious.  The UI was not being taken to an error page (as what happens on ItemUpdating() ) it simply returned back to the list view. 

     

    Thanks for your input.