locked
ItemUpdating - Not able to get the updated field value on itemupdating RRS feed

  • Question

  • Hi All,
      
        I'm overriding the ItemUpdating event, here I have a field "Start Date" . Now when user edits the Start date of an item and clicks "OK",I need to capture the previous and latest start date value and compare them.

    I'm able to get the previous date value by the following code

    <i>
                   SPWeb contextWeb = properties.OpenWeb();
                    SPList spList = contextWeb.Lists[properties.ListId];
                    SPListItem contextItem = spList.GetItemById(properties.ListItemId);


                    current_id = Convert.ToInt32(contextItem["ID"]);

                    if (contextItem["Start Date"] != null)
                        current_StartDate = (DateTime)contextItem["Start Date"];

    </i>

    But am not able to get the latest(committed) Start date.

    I tried using the AfterProperty, but no luck :(


    <i>
                 if (properties.AfterProperties["Start Date"] != null)
                        updated_StartDate = Convert.ToDateTime(properties.AfterProperties["Start Date"]);

    </i>


    Can anyone help me in finding the solution for the same

    With regards
    Biju

    Wednesday, May 20, 2009 6:43 AM

Answers

  • Hi,
    I got  the solution......... I tried using the internal name and it worked :)


    //Get Current Date               
    SPWeb contextWeb = properties.OpenWeb();
    SPList spList = contextWeb.Lists[properties.ListId];
    SPListItem contextItem = spList.GetItemById(properties.ListItemId);

    current_id = Convert.ToInt32(contextItem["ID"]);

    if (contextItem["Start Date"] != null)
       current_StartDate = (DateTime)contextItem["Start Date"];


    // Get Updated Date
    if (properties.AfterProperties[contextWeb.Lists[properties.ListId].Fields["Start Date"].InternalName]!=null)
            updated_StartDate = Convert.ToDateTime(properties.AfterProperties[contextWeb.Lists[properties.ListId].Fields["Start Date"].InternalName]);




    Thankyou all for the great support......

     

     

    • Marked as answer by Biju.VA18 Thursday, May 21, 2009 11:40 AM
    Thursday, May 21, 2009 11:40 AM

All replies

  • Biju,

    It's been a while since I've coded this stuff, but... I think the problem is:

    1. When assigning a value to a field, reference the field directly (you're doing this correctly):
      properties.AfterProperties["Start Date"] = (DateTime)dateTimeVariable;
    2. When extracting the value of a field, convert it to a string first:
      updated_StartDate = Convert.ToDateTime(properties.AfterProperties["Start Date"].ToString());

    I hope that's right. I don't have a development machine in front of me.


    http://sharepointgear.wordpress.com/
    Wednesday, May 20, 2009 7:00 AM
  • >>if (properties.AfterProperties["Start Date"] != null)
    >>                    updated_StartDate = Convert.ToDateTime(properties.AfterProperties["Start Date"]);

    As Fodi says, you need to convert to string first, so it would be:

    if (properties.AfterProperties["Start Date"] != null)
                        updated_StartDate = Convert.ToDateTime(properties.AfterProperties["Start Date"].ToString());

    Hope that helps
    Wednesday, May 20, 2009 7:07 AM
  • Hi Biju.va,

    You can access the current list item by doing the following (it will save you some lines of code...):

    SPListItem item = properties.ListItem;


    You also can access the value of the previous value of the column by:

    DateTime oldstartDate = Convert.ToDateTime(properties.BeforeProperties["Start Date"].ToString());

    You may be able to get the current value of the start date by (havn't tested it):

    DateTime newStartDate = Convert.ToDateTime(item["Start Date"].ToString());

    or

    DateTime newStartDate = Convert.ToDateTime(properties.AfterProperties["Start Date"].ToString());


    Also if you can be more specific on what value you get on the latest start date?


    Let me know if that helps...







    Wednesday, May 20, 2009 7:26 AM
  • Hi Chakkaradeep,

      I tried your solution, but still no luck :(

    if

     

    (properties.AfterProperties["Start Date"] != null)

    updated_StartDate =

    Convert.ToDateTime(properties.AfterProperties["Start Date"].ToString());



      Am getting the updated date as "1/1/0001 12:00:00 AM"
    Wednesday, May 20, 2009 7:47 AM
  • Hi Fodi,

      I tried your solution, but still no luck :(

    if

     

    (properties.AfterProperties["Start Date"] != null)
    updated_StartDate =
    Convert.ToDateTime(properties.AfterProperties["Start Date"].ToString());



      Am getting the updated date as "1/1/0001 12:00:00 AM"
    Wednesday, May 20, 2009 7:50 AM
  • hi Amir,

    I tried your solution but no luck :( 


    DateTime oldstartDate = Convert.ToDateTime(properties.BeforeProperties["Start Date"].ToString()); ---  Returns null vlaue


    DateTime newStartDate = Convert.ToDateTime(item["Start Date"].ToString());---- Returns me the previous value


    DateTime newStartDate = Convert.ToDateTime(properties.AfterProperties["Start Date"].ToString());---- Returns me as "1/1/0001 12:00:00 AM"

    Wednesday, May 20, 2009 7:56 AM
  • Can you check writing the code in the ItemUpdated event?

    And also it is good to try casting it as DateTime variable

    DateTime updatedStartDate  = properties.AfterProperties["Start Date"] as DateTime;


    Regards, Chakkaradeep http://www.chakkaradeep.com
    Wednesday, May 20, 2009 7:57 AM
  • Biju,

    What value do you get if you don't cast it to a DateTime and just assign it to a string? If you're unable to debug, just write the value out to a text field?
    http://sharepointgear.wordpress.com/
    Wednesday, May 20, 2009 8:04 AM
  • You may want to try using the internal name when using the properties object since there is a space in the name you may try referencing by "Start_x0020_Date"

    • Proposed as answer by Shai Petel Friday, March 8, 2013 2:38 PM
    Wednesday, May 20, 2009 8:32 AM
  • hi,


     i tried it but i am getting an error as "The as operator must be used with a reference type ('System.DateTime' is a value type) "
    :(


    updated_StartDate = properties.AfterProperties[

    "Start Date"] as DateTime;

    Wednesday, May 20, 2009 10:23 AM
  • Fodi,

    I tried by just assigning it to a string. It results in a empty string :( .

    string

     

    str_updated_StartDate=string.Empty;

     

     

    if (properties.AfterProperties["Start Date"] != null)

    str_updated_StartDate = properties.AfterProperties[

     

    "Start Date"].ToString();

    Wednesday, May 20, 2009 10:32 AM
  • Amir,


    I tried using the internal name,still no luck :(

    (properties.AfterProperties["Start Dadsfte"] != null) 
    updated_StartDate =C
    onvert.ToDateTime(properties.AfterProperties[contextWeb.Lists[properties.ListId].Fields["Start Date"].InternalName]);//Convert.ToDateTime(properties.AfterProperties["Start Date"].ToString());

    if

    it returns the  date as  1/1/0001 12:00:00 AM1/1/0001 12:00:00 AM

     



    Wednesday, May 20, 2009 10:39 AM
  • Chandran,

       I tried casting the object as DateTime, but still no luck.

       I want to write my remaining logic in the ItemUpdating event, where i need to update some of the related items based on the start date.

    Wednesday, May 20, 2009 11:22 AM
  • Okay,

    Well, if the string is coming up empty, there's no use trying to cast it to a DateTime object. The value has always been empty. That's why you're getting the default DAteTime object value: 1/1/0001.....

    If I remember correctly, the afterproperties collection is actually a Dictionary object and a key\value pair will only be created for values that have been modified. So, if the Start Date isn't modified in the update, then properties.AfterProperties["Start Date"]  will be null and you won't assign a new value. To prove this, try:

    str_updated_StartDate="Cheese Burger";  

    if (properties.AfterProperties["Start Date"] != null)
    str_updated_StartDate = properties.AfterProperties[
    "Start Date"].ToString();

    Check if the final string is actually "Cheese Burger". Or, keep your existing code, but make a change to the list item and see if the passed properly.

    - fodi


    http://sharepointgear.wordpress.com/
    Wednesday, May 20, 2009 11:41 AM
  • Hi Biju,

    >>I need to capture the previous and latest start date value and compare them

    Since you need to get the new value which was edited by the user, I presume you should go for ItemUpdated event, as only that gets fired up after data being committed to the database. You could just try once to see whether you can get the correct values in the ItemUpdated event, and you can still change some of the related items that you wanted to change based on the start date.

    Hope that helps
    Regards, Chakkaradeep http://www.chakkaradeep.com
    Wednesday, May 20, 2009 9:40 PM
  • Chakkaradeep,

    The ItemUpdating() receiver will also provide access to the modified value. Biju must use this receiver if he wants to set a value before the list item is updated. Furthermore, if he uses the ItemUpdated event, he won't know what the value was before the update, so he won't be able to make a comparison.

    Biju, I think you're on the right track.

    - fodi
    http://sharepointgear.wordpress.com/
    Wednesday, May 20, 2009 11:37 PM
  • Hi Biju.va,

    In the Convert function I didn't see you using the internal  name. 
    • Proposed as answer by Varun Jaswal Thursday, March 10, 2011 11:44 AM
    • Unproposed as answer by Varun Jaswal Thursday, March 10, 2011 11:47 AM
    • Proposed as answer by Shai Petel Friday, March 8, 2013 2:37 PM
    Wednesday, May 20, 2009 11:53 PM
  • Hi,
    I got  the solution......... I tried using the internal name and it worked :)


    //Get Current Date               
    SPWeb contextWeb = properties.OpenWeb();
    SPList spList = contextWeb.Lists[properties.ListId];
    SPListItem contextItem = spList.GetItemById(properties.ListItemId);

    current_id = Convert.ToInt32(contextItem["ID"]);

    if (contextItem["Start Date"] != null)
       current_StartDate = (DateTime)contextItem["Start Date"];


    // Get Updated Date
    if (properties.AfterProperties[contextWeb.Lists[properties.ListId].Fields["Start Date"].InternalName]!=null)
            updated_StartDate = Convert.ToDateTime(properties.AfterProperties[contextWeb.Lists[properties.ListId].Fields["Start Date"].InternalName]);




    Thankyou all for the great support......

     

     

    • Marked as answer by Biju.VA18 Thursday, May 21, 2009 11:40 AM
    Thursday, May 21, 2009 11:40 AM