SharePointCommunity
Die deutschsprachige Community für SharePoint, Microsoft 365, Teams, Yammer und mit Azure
SharePoint EventReceiver and DateTime values

Blogs

Oliver Wirkus

Syndication

Certifications

Web 2.0

http://smits-net.de/img/linkedin_button.gif

XING

http://i80.photobucket.com/albums/j191/mikesamerica/twitter_button_zpsee74f711.png

 

 Add to Technorati Favorites

 

 Delicious Bookmark this on Delicious

 

  Locations of visitors to this page

 

In my current project I created an EventReceiver that had been attached to a generic list. This list  has some fields (as always) and one of them is a DateTime field.

The EventReceiver gets triggered on ItemUpdating and should check the value in the DateTime field. When a user changed an item in the generic list the EventReceiver gets called like this:

image

 

When I tried to access the DateTime field I noticed that the value is saved in UTC format:

image

 

To do some checks and comparisons with the field's value I needed to convert the value from UTC to local time - but in consideration of the current web's timezone settings!

The time zone settings of the current web could look like this (as an example):

image

Although the locale is set to german, the time zone could be have different (not-german) settings.

The challenge was to convert the UTC time to a local time matching the current web's time zone settings.

After a lot of trying I finally came up with this solution:

image

Interessting to know: SharePoint is handling DateTime values in a very similar manner. Just try this: add an item to a list that has at least one DateTime field. Now change the web's time zone settings to another time zone. When you switch back to the SharePoint list you'll notice that SharePoint 'changed' the value of the datetime field.

This is how the list item looks like when the web's time zone is set to UTC+01:00 Amsterdam,Berlin,Bern:

image

And now I change the web's time zone to UTC-03:00 Buenos Aires:

image

As you can see the time 'changed' due to a change in the time zone.

So - handling of DateTime values by code in SharePoint is not as easy as you might exspect ;-)


Bereitgestellt 21 Mai 2014 15:24 von Oliver Wirkus
Gespeichert unter: , ,