« Back to Log

SharePoint list item and its ItemAdding event

Posted by myself on April 01, 2009

I've found many solutions and discussions trying to find one about the ItemAdding event for a SharePoint list item on the internet, neither of which fixed my problem. Actually I've had two. 

The first one was that you have to set the changes made to the ChangedProperties property that belongs the AfterProperties property. If you try to set them on the AfterProperties directly (why you can do this I don't know) the changes will be ignored when creating the item.

The second problem was that all values at the point of that event are stored as strings, they are not yet converted to their respective classes. In my case I needed to set the end date of an event list item 45 minutes after the event date. The following code snippets shows how to achive this. Basically you first have to convert the string into a DateTime object, add the 45 minutes to it and convert the DateTime object back to a valid string representation.


public override void ItemUpdating(SPItemEventProperties properties)
{
    base.ItemUpdating(properties);

    DateTime eventDate =
        SPUtility.CreateDateTimeFromISO8601DateTimeString(properties.AfterProperties["EventDate"].ToString());

    properties.AfterProperties.ChangedProperties["EndDate"] =
        eventDate.AddMinutes(45).ToString("o");
}

No comments registered

Random Quote

Versuchungen sollte man nachgeben. Wer weiss ob sie wiederkommen. (Wilde)

» ...

koffeinfrei on identi.ca

icon
2010-07-26 11:46:49
Everybody needs a fishbowl: “YouTube - Barry Schwartz: The paradox of choice” — http://www.youtube.com/watch?v=VO6XEQIsCoM
icon
2010-06-18 09:07:19
@kentbrew: what i'm i missing? the #identica-badge doesn't show my entries on http://koffeinfrei.org/
icon
2010-06-16 08:00:30
“YouTube - BP Spills Coffee” — http://www.youtube.com/watch?v=2AAa0gd7ClM
icon
2010-06-01 20:03:29
"The surprising truth about what motivates us”: http://www.youtube.com/watch?v=u6XAPnuFjJc&feature=player_embedded
icon
2010-05-20 21:11:48
“Sumatra PDF is a slim, free, open-source PDF viewer for Windows” — http://blog.kowalczyk.info/software/sumatrapdf/index.html

Ying and Yang

where I am
where I should be