Pregunta

I have a SharePoint list having couple of DateTime properties.
Currently, we are not manipulating any date-time values when saving list-item to SharePoint. It was working fine but recently we noticed that when retrieving, datetime values are being converted to UTC time.

One solution could be to convert the values to local time zone while retrieving as -

    //1.Retrieve SharePoint TimeZone
    var spTimeZone = _sharePointContextHandler.GetSharePointTimeZone();

    //2.Resolve System.TimeZoneInfo from Microsoft.SharePoint.Client.TimeZone 
    var fixedTimeZoneName = spTimeZone.Description.Replace("and", "&");
    var timeZoneInfo = TimeZoneInfo.GetSystemTimeZones().FirstOrDefault(tz => tz.DisplayName == fixedTimeZoneName);

    //3.Convert time retrieved from SP
    DateTime dtUtc = Convert.ToDateTime(documentItem["ColumnName"]);
    var dtLocalTimeZone = TimeZoneInfo.ConvertTimeFromUtc(dtUtc, timeZoneInfo);    

Three questions -
1. Does SharePoint returns all datetime values in UTC?
2. What will be the best approach to address this - make such conversion while saving or while retrieving? Or is there any other way to address this without making any code changes?
3. If I just need to convert time retrieved from SP to local zone, can I safely replace above code with this -

    //Convert time retrieved from SP
    DateTime dtUtc = Convert.ToDateTime(documentItem["ColumnName"]);
    var dtLocalTimeZone = TimeZoneInfo.ConvertTimeFromUtc(dtUtc, TimeZoneInfo.Local);  

Thank you!

¿Fue útil?

Solución

Sharepoint will always store the value as UTC and will always serve it as UTC via web service calls, it is up to you to render it in the proper format. Native forms render them based off your regional settings, if you are rolling your own solution, you need to fetch those and adjust the time accordingly.

The code provided should work to convert times appropriately.

Licenciado bajo: CC-BY-SA con atribución
scroll top