Setting Timezone in SharePoint Using CSOM (Client-Side Object Model)
In SharePoint Online (SPO), the default time zone is typically set at the site collection level, but there is no direct, built-in CSOM (Client-Side Object Model) API method to retrieve the default time zone.
However, we can retrieve the regional settings of a site collection, which includes the time zone setting, using CSOM in SharePoint Online. To achieve this, we would need to use the RegionalSettings property of a site object.
When working with SharePoint Online, one of the key settings we may need to adjust is the timezone. This ensures that time-based actions like calendar events, alerts, and workflows are displayed correctly for the users based on their location.
SharePoint allows us to set the timezone at the site collection level. This means we can define the timezone for individual site collections, which is especially useful when ours organization operates across different regions and time zones.
In this blog post, we’ll walk through how to set the timezone for a SharePoint site collection using the CSOM (Client-Side Object Model) in C#.
What is CSOM?
CSOM is a set of libraries that allow developers to interact with SharePoint data and settings remotely. It’s a great tool for managing SharePoint Online without the need for direct access to the SharePoint server. CSOM can be used to create, update, and retrieve SharePoint site collections, lists, documents, and even manage settings like time zone configuration.
Pre-requisites
Before we start coding, we need to ensure the following:
- we need SharePoint Online access with Site Collection Administrator permissions.
- Ensure we have the Microsoft.SharePoint.Client and Microsoft.SharePoint.Client.Runtime assemblies available in our project. we can install them via NuGet package manager.
Install-Package Microsoft.SharePointOnline.CSOM
Steps to Set Timezone in SharePoint Using CSOM
In SharePoint, we can set the TimeZone for a site collection via the Regional Settings. The TimeZone is part of the RegionalSettings object in CSOM. The steps below will guide through the process of setting the timezone for a SharePoint site collection.
Code Example to Set Timezone Using CSOM in C#
using Microsoft.SharePoint.Client;
using System;
namespace SetTimezoneInSharePointCSOM
{
class Program
{
static void Main(string[] args)
{
// Define the SharePoint site URL
string siteUrl = "https://yourtenantname.sharepoint.com/sites/YourSite";
string username = "yourusername"; // SharePoint username
string password = "yourpassword"; // SharePoint password
// Set the timezone ID
// You can find the full list of time zone IDs at: https://docs.microsoft.com/en-us/previous-versions/office/sharepoint-server/ee538256(v=office.14)
// Example: (GMT-05:00) Eastern Time (US and Canada)
int timeZoneId = 10; // (GMT-05:00) Eastern Time (US and Canada)
// Create a new client context and authenticate
ClientContext context = new ClientContext(siteUrl);
context.Credentials = new SharePointOnlineCredentials(username, password);
// Load the current site's regional settings
Web web = context.Web;
context.Load(web, w => w.RegionalSettings);
context.ExecuteQuery();
// Set the timezone
web.RegionalSettings.TimeZone = context.Site.TimeZones.GetById(timeZoneId);
// Update the regional settings
web.Update();
context.ExecuteQuery();
// Output the result
Console.WriteLine($"Time Zone set to ID: {timeZoneId} for site collection: {siteUrl}");
}
}
}
Explanation of Code
- ClientContext:
- We begin by creating a ClientContext for connecting to SharePoint Online. This object allows us to interact with the SharePoint site collection and its settings.
- Credentials:
- The credentials (
SharePointOnlineCredentials
) are used to authenticate the user. we must replace"yourusername"
and"yourpassword"
with our actual SharePoint Online account credentials.
- The credentials (
- Web Object:
- The Web object represents the site collection in SharePoint. We load the RegionalSettings property of the site collection to access its regional settings.
- TimeZone:
-
The time zone is set using the TimeZones.GetById(timeZoneId) method. we must use the appropriate TimeZoneId that corresponds to the timezone we want. In the example, we set it to 10, which is the UTC (Coordinated Universal Time).
-
To find the full documentation of timezone, refer to the Microsoft Documentation.
-
- Update:
- The
web.Update()
method updates the RegionalSettings object with the new time zone.
- The
- ExecuteQuery:
- The ExecuteQuery method sends the request to SharePoint Online to apply the changes.
Valid TimeZone IDs
Here is a list of timezone IDs, we can use in the above example:
ID | Time Zone |
---|---|
2 | (GMT) Greenwich Mean Time : Dublin, Edinburgh, Lisbon, London |
3 | (GMT+01:00) Brussels, Copenhagen, Madrid, Paris |
4 | (GMT+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna |
5 | (GMT+02:00) Athens, Bucharest, Istanbul |
6 | (GMT+01:00) Belgrade, Bratislava, Budapest, Ljubljana, Prague |
7 | (GMT+02:00) Minsk |
8 | (GMT-03:00) Brasilia |
9 | (GMT-04:00) Atlantic Time (Canada) |
10 | (GMT-05:00) Eastern Time (US and Canada) |
11 | (GMT-06:00) Central Time (US and Canada) |
12 | (GMT-07:00) Mountain Time (US and Canada) |
13 | (GMT-08:00) Pacific Time (US and Canada) |
14 | (GMT-09:00) Alaska |
15 | (GMT-10:00) Hawaii |
16 | (GMT-11:00) Midway Island, Samoa |
17 | (GMT+12:00) Auckland, Wellington |
18 | (GMT+10:00) Brisbane |
19 | (GMT+09:30) Adelaide |
20 | (GMT+09:00) Osaka, Sapporo, Tokyo |
21 | (GMT+08:00) Kuala Lumpur, Singapore |
22 | (GMT+07:00) Bangkok, Hanoi, Jakarta |
23 | (GMT+05:30) Chennai, Kolkata, Mumbai, New Delhi |
24 | (GMT+04:00) Abu Dhabi, Muscat |
25 | (GMT+03:30) Tehran |
26 | (GMT+03:00) Baghdad |
27 | (GMT+02:00) Jerusalem |
28 | (GMT-03:30) Newfoundland |
29 | (GMT-01:00) Azores |
30 | (GMT-02:00) Mid-Atlantic |
31 | (GMT) Casablanca, Monrovia, Reykjavik |
32 | (GMT-03:00) Buenos Aires, Georgetown |
33 | (GMT-04:00) Caracas, La Paz |
34 | (GMT-05:00) Indiana (East) |
35 | (GMT-05:00) Bogota, Lima, Quito, Rio Branco |
36 | (GMT-06:00) Saskatchewan |
37 | (GMT-06:00) Guadalajara, Mexico City, Monterrey |
38 | (GMT-07:00) Arizona |
39 | (GMT-12:00) International Date Line West |
40 | (GMT+12:00) Fiji Is., Kamchatka, Marshall Is. |
41 | (GMT+11:00) Magadan, Solomon Is., New Caledonia |
42 | (GMT+10:00) Hobart |
43 | (GMT+10:00) Guam, Port Moresby |
44 | (GMT+09:30) Darwin |
45 | (GMT+08:00) Beijing, Chongqing, Hong Kong S.A.R., Urumqi |
46 | (GMT+06:00) Almaty, Novosibirsk |
47 | (GMT+05:00) Islamabad, Karachi, Tashkent |
48 | (GMT+04:30) Kabul |
49 | (GMT+02:00) Cairo |
50 | (GMT+02:00) Harare, Pretoria |
51 | (GMT+03:00) Moscow, St. Petersburg, Volgograd |
53 | (GMT-01:00) Cape Verde Is. |
54 | (GMT+04:00) Baku |
55 | (GMT-06:00) Central America |
56 | (GMT+03:00) Nairobi |
57 | (GMT+01:00) Sarajevo, Skopje, Warsaw, Zagreb |
58 | (GMT+05:00) Ekaterinburg |
59 | (GMT+02:00) Helsinki, Kyiv, Riga, Sofia, Tallinn, Vilnius |
60 | (GMT-03:00) Greenland |
61 | (GMT+06:30) Yangon (Rangoon) |
62 | (GMT+05:45) Kathmandu |
63 | (GMT+08:00) Irkutsk, Ulaan Bataar |
64 | (GMT+07:00) Krasnoyarsk |
65 | (GMT-04:00) Santiago |
66 | (GMT+05:30) Sri Jayawardenepura |
67 | (GMT+13:00) Nuku’alofa |
68 | (GMT+10:00) Vladivostok |
69 | (GMT+01:00) West Central Africa |
70 | (GMT+09:00) Yakutsk |
71 | (GMT+06:00) Astana, Dhaka |
72 | (GMT+09:00) Seoul |
73 | (GMT+08:00) Perth |
74 | (GMT+03:00) Kuwait, Riyadh |
75 | (GMT+08:00) Taipei |
76 | (GMT+10:00) Canberra, Melbourne, Sydney |
77 | (GMT-07:00) Chihuahua, La Paz, Mazatlan |
78 | (GMT-08:00) Tijuana, Baja California |
79 | (GMT+02:00) Amman |
80 | (GMT+02:00) Beirut |
81 | (GMT-04:00) Manaus |
82 | (GMT+03:00) Tbilisi |
83 | (GMT+02:00) Windhoek |
84 | (GMT+04:00) Yerevan |
Testing the Changes
After running the above code, the timezone for the site collection will be updated. we can verify the changes by navigating to the Regional Settings in the Site Settings of our SharePoint site collection:
- Go to SharePoint site.
- Navigate to Site Settings > Regional Settings.
- Check that the Time Zone is correctly set as per we change.
Conclusion
Changing the time zone in SharePoint Online is a simple but powerful feature, especially for organizations operating across different time zones. By using CSOM, we can automate and customize our SharePoint configuration, including setting the time zone for individual site collections.
While this demonstrates how to set the time zone for a site collection, we can easily extend this code to handle multiple site collections or even dynamically set time zones based on user preferences or geographical location.
Leave a comment