Is it possible to get the timezone of a Sharepoint site programmatically using Microsoft.SharePoint.Client in C#? I need the Sharepoint site timezone to match it with a particular timezone.
3 Answers
Unfortunately for the Client Side Object Model of SharePoint it is not possible to get the timezone perse. Server Object Model and SPServices contains a property for SPWeb called RegionalSettings, however this is lacking in CSOM.
The good thing here is that CSOM has a Utility feature called FormatDateTime
which you can use to convert a specific string/date to the timezone of your site.
Below is an example of it's use in a simple console application:
ClientContext clientContext = new ClientContext("http://yoursite.com");
Site site = clientContext.Site;
DateTime dt = DateTime.Parse("04/24/2013 5:44PM").ToUniversalTime();
ClientResult<string> cr = Utility.FormatDateTime(clientContext, clientContext.Web, dt, DateTimeFormat.DateTime);
clientContext.ExecuteQuery();
string value = cr.ToString();
DateTime webdt = DateTime.Parse(cr.Value.ToString());
Console.WriteLine(webdt.ToString());
Console.Read();
Take note that you need to have the SharePoint Client DLLs included in the project as well as referencing them properly. Hope this helps.

- 1,274
- 12
- 25
ClientContext context = new ClientContext(yourSite);
var culture = context.Web.RegionalSettings;
context.Load(culture);
var tz = culture.TimeZone;
context.Load(tz);
context.ExecuteQuery();
context.Dispose();
tz will contain your timezone, bias, etc..

- 333
- 3
- 9
-
RegionalSettings is not a property of ClientContext.Web. This code snippet gives me a syntax error. And of course I've referenced the Sharepoint Client DLLs. – bittusarkar Jan 01 '14 at 17:50
-
1http://msdn.microsoft.com/en-Us/library/microsoft.sharepoint.client.regionalsettings.aspx The fact you did not find it is probably because you are using SP2010. – Deptor Jan 27 '14 at 09:55
My code is similar to Deptor's, but I think my code fixes a few syntax issues I had when I tried running it on my SP 2013 site. I also use some global variables to store things like the ClientContext, Web, Culture, and Timezone SP CSOM objects to help learners understand what each object contains.
Code:
var context, web, culture, tz;
var siteTzId, siteTzDesc, siteTzInformation;
getSiteRegionalTimeZone();
setTimeout(function(){
alert("Site TZ = |"+ siteTzDesc +"|");
}, 2000);
function getSiteRegionalTimeZone() {
context = new SP.ClientContext();
web = context.get_web();
culture = web.get_regionalSettings();
context.load(culture);
tz = culture.get_timeZone();
context.load(tz);
context.executeQueryAsync(function(){
siteTzId = tz.$5_0.$H_0.Id;
siteTzDesc = tz.$5_0.$H_0.Description;
siteTzInformation = tz.$5_0.$H_0.Information;
});
context.dispose();
};

- 1
- 2