How to Set Time Zone in Office 365 using CSOM ?

In Office 365, we can set the Time zone settings for a site collection under Regional Settings. You always provide time zone while creating a site Collection, In this article, I’m providing you the sample code to set the Time Zone Settings using Client Side object model.

// Get access to source site
using (ClientContext context = new ClientContext(“https://my.sharepoint.com/sites/”))
{
SecureString passWord = new SecureString();

foreach (char c in “*****”.ToCharArray()) passWord.AppendChar(c);

context.Credentials = new SharePointOnlineCredentials(“yourID@my.onmicrosoft.com”, passWord);

// Actual code for Getting Client Context
Web web = context.Web;
context.Load(web,
webSite => webSite.Title);

context.ExecuteQuery();
RegionalSettings regSettings = web.RegionalSettings;
context.Load(web);
context.Load(regSettings);

context.ExecuteQuery();

Microsoft.SharePoint.Client.TimeZone utcTimeZone = clientContext.Site.RootWeb.RegionalSettings.TimeZones.Where(timezone => timezone.Description == Constants.UtcTimeZone).FirstOrDefault();
if (utcTimeZone != null)
{

// Set the Time Zone
clientContext.Site.RootWeb.RegionalSettings.TimeZone = utcTimeZone;
clientContext.Site.RootWeb.RegionalSettings.Update();

context.ExecuteQuery();

}

Please read my Previous article to know about getting the time zone using CSOM code.

Happy SharePointing folks 🙂 Hope it heps!!

Leave a Reply

Your email address will not be published. Required fields are marked *