{"id":2151,"date":"2018-11-18T11:00:57","date_gmt":"2018-11-18T11:00:57","guid":{"rendered":"https:\/\/developer.microsoft.com\/en-us\/office\/blogs\/?p=2151"},"modified":"2018-11-18T11:00:57","modified_gmt":"2018-11-18T11:00:57","slug":"30daysmsgraph-day-18-use-case-update-user-mailbox-settings","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/30daysmsgraph-day-18-use-case-update-user-mailbox-settings\/","title":{"rendered":"30DaysMSGraph \u2013 Day 18 \u2013 Use Case: Update User Mailbox Settings"},"content":{"rendered":"<p><a href=\"https:\/\/aka.ms\/30DaysMSGraph\">List of all posts in the #30DaysMSGraph series<\/a><\/p>\n<p>In <a href=\"https:\/\/developer.microsoft.com\/en-us\/graph\/blogs\/30daysmsgraph-day-17-use-case-assign-license\">Day 17<\/a> we extended the base .Net Core console application to assign license to a user in Azure AD. Today we\u2019ll extend the base console application to update the users (exchange online) mailbox settings in Office 365.\u00a0 Note that it is not required to perform each of the daily Use Case samples in order to continue to the next.\u00a0 Starting with the base console application from Day 15 will be sufficient if building from scratch, or as always, you can clone the completed version in the provided repo at bottom of this post. The application uses a client secret and the Microsoft Authentication Library (MSAL) to establish an authentication context with the Microsoft Graph API.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2018\/11\/30DaysMSGraph_Day18_Source.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-2154\" src=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2018\/11\/30DaysMSGraph_Day18_Source.png\" alt=\"\" width=\"1280\" height=\"462\" srcset=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2018\/11\/30DaysMSGraph_Day18_Source.png 1280w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2018\/11\/30DaysMSGraph_Day18_Source-300x108.png 300w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2018\/11\/30DaysMSGraph_Day18_Source-1024x370.png 1024w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2018\/11\/30DaysMSGraph_Day18_Source-768x277.png 768w\" sizes=\"(max-width: 1280px) 100vw, 1280px\" \/><\/a><\/p>\n<p>Today we will explore the process of updating a user mailbox in preparation for onboarding a user.<\/p>\n<h3>Retrieve and Update User Mailbox Settings<\/h3>\n<p>In preparation for onboarding a user, once the user is created and appropriate license is applied, now we can update the user specific mailbox settings like the users time zone, locale info, working hours etc.<\/p>\n<p>You can use the Microsoft Graph endpoint \/users\/{id|userPrincipalName}\/mailboxSettings to access a users mailbox settings. You can find the Microsoft Graph documentation to retrieve mailbox settings <a href=\"https:\/\/developer.microsoft.com\/en-us\/graph\/docs\/api-reference\/v1.0\/api\/user_get_mailboxsettings\">here<\/a> and the documentation to update mailbox settings <a href=\"https:\/\/developer.microsoft.com\/en-us\/graph\/docs\/api-reference\/v1.0\/api\/user_update_mailboxsettings\">here<\/a>.<\/p>\n<p>To read and update users mailbox settings it requires new permissions. As such the Azure AD App Registration\u00a0needs to be updated to include the following application permissions: User.Read.All, MailboxSettings.Read, MailboxSettings.ReadWrite.<\/p>\n<p>This is optional: in addition to the above lets add Mail.Read application permission too as the code sample showcases how to use Microsoft Graph SDK to retrieve users inbox messages too.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2018\/11\/blog-app-perms.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-2153\" src=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2018\/11\/blog-app-perms.png\" alt=\"\" width=\"1075\" height=\"406\" srcset=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2018\/11\/blog-app-perms.png 1075w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2018\/11\/blog-app-perms-300x113.png 300w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2018\/11\/blog-app-perms-1024x387.png 1024w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2018\/11\/blog-app-perms-768x290.png 768w\" sizes=\"(max-width: 1075px) 100vw, 1075px\" \/><\/a><\/p>\n<p>With the necessary permissions configured it is time to look at new the code required.<\/p>\n<p>Get and Set User Mailbox Default Time zone<\/p>\n<p>Below you will see the two helper methods that will get and set the user mailbox default time zone.<\/p>\n<div>\n<pre style=\"padding-left: 30px\">public async Task&lt;string&gt; GetUserMailboxDefaultTimeZone(string alias)\n{\n    User user = FindByAlias(alias).Result;\n    User detailedUser = await _graphClient.Users[user.Id].Request().Select(\"MailboxSettings\").GetAsync();\n    return detailedUser.MailboxSettings.TimeZone;\n}<\/pre>\n<div>The above method uses Microsoft Graph SDK to retrieve the supplied users current time zone setting.<\/div>\n<\/div>\n<div><\/div>\n<div>\n<div>\n<pre style=\"padding-left: 30px\">public async void SetUserMailboxDefaultTimeZone(string alias, string timezone)\n{\n   User user = FindByAlias(alias).Result;\n   Uri Uri = new Uri(\"https:\/\/graph.microsoft.com\/v1.0\/users\/\"+ user.Id +\"\/mailboxSettings\");\n   String jsonContent = \"{\\\"timeZone\\\" : \\\"\"+ timezone +\"\\\"}\";\n   HttpContent httpContent = new StringContent(jsonContent, System.Text.Encoding.UTF8, \"application\/json\");\n   await _httpClient.PatchAsync(Uri, httpContent);\n}<\/pre>\n<\/div>\n<div>Above method takes the user alias and the timezone value that must be applied to the users mailbox.<\/div>\n<div>Note: An important thing to note here is that this method uses HttpClient instead of using Microsoft Graph SDK to update the mailbox settings. The reason for this is because as of today (Nov 18th 2018) there is no support to update MailboxSettings using the SDK. This capability is under work and it will be available soon.<\/div>\n<\/div>\n<div><\/div>\n<div>Now that the helper methods are ready. You can call into these methods from the Program.cs class using below method:<\/div>\n<div>\n<div>\n<pre style=\"padding-left: 30px\">private static void GetUserMailboxDefaultTimeZone()\n{\n\u00a0\u00a0\u00a0const string alias = \"admin\";\n\u00a0\u00a0\u00a0var mailboxHelper = new MailboxHelper(_graphServiceClient);\n\u00a0\u00a0\u00a0var defaultTimeZone = mailboxHelper.GetUserMailboxDefaultTimeZone(alias).Result;\n\u00a0\u00a0\u00a0Console.WriteLine(\"Default timezone: \"+ defaultTimeZone);\n}\n\nprivate static void SetUserMailboxDefaultTimeZone()\n{\n\u00a0\u00a0\u00a0const string alias = \"admin\";\n\u00a0\u00a0\u00a0var mailboxHelper = new MailboxHelper(_graphServiceClient, _httpClient);\n\u00a0\u00a0\u00a0mailboxHelper.SetUserMailboxDefaultTimeZone(alias, \"Eastern Standard Time\");\n}<\/pre>\n<div>Note the SetUserMailboxefaultTimeZone method is initiating the MailboxHelper with both the _graphServiceClient and _httpClient. The reason for this is because the\u00a0SetUserMailboxDefaultTimeZone in the MailboxHelper.cs is using both the client objects. The GraphServiceClient object is used to find the user using the alias. The HttpClient object is used to update the mailboxsettings.<\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<h3>Create and Retrieve User Mailbox Message Rules<\/h3>\n<div>The MailboxHelper class has additional methods that will showcase how to use Microsoft Graph SDK to retrieve user messages, create and retrieve message rules. Below is code snippet that encapsulates creation and retrieving of mailbox message rules.<\/div>\n<div>\n<div>\n<pre style=\"padding-left: 30px\">private static MessageRule BuildMailRule(string displayName, int sequence, bool isEnabled, string senderContains, string forwardToEmail)\n{\n\u00a0\u00a0\u00a0\u00a0IEnumerable&lt;string&gt; senderContainsList = new string[]{senderContains};\n\u00a0\u00a0\u00a0\u00a0EmailAddress email = new EmailAddress(){\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Address = forwardToEmail\n            };\n    Recipient recipient = new Recipient(){\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 EmailAddress = email\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 };\n\u00a0\u00a0\u00a0\u00a0IEnumerable&lt;Recipient&gt; recipientList = new Recipient[]{ recipient };\n\u00a0\u00a0\u00a0\u00a0var msgRule = new MessageRule{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DisplayName = displayName,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Sequence = sequence,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 IsEnabled = isEnabled,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Conditions = new MessageRulePredicates{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 SenderContains = senderContainsList\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0    },\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0    Actions = new MessageRuleActions{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ForwardTo = recipientList\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0    }\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 };\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return msgRule;\n}<\/pre>\n<div>Above is the helper method that takes various parameters needed to create a new message rule.<\/div>\n<\/div>\n<\/div>\n<div>\n<div>\n<pre style=\"padding-left: 30px\">public async Task CreateRule(string alias, string displayName, int sequence, bool isEnabled, string senderContains, string forwardToEmail)\n{\n\u00a0\u00a0\u00a0\u00a0MessageRule rule = BuildMailRule(displayName, sequence, isEnabled, senderContains, forwardToEmail);\n\u00a0\u00a0\u00a0\u00a0User user = FindByAlias(alias).Result;\n\u00a0\u00a0\u00a0\u00a0await _graphClient.Users[user.Id].MailFolders.Inbox.MessageRules.Request().AddAsync(rule);\n}<\/pre>\n<div>Above method users the BuildMailRule helper method to create a new MessageRule object and then uses Microsoft Graph SDK to add the rule to the users mailbox.<\/div>\n<div>\n<div>\n<pre style=\"padding-left: 30px\">public async Task&lt;List&lt;ResultsItem&gt;&gt; GetUserMailboxRules(string alias)\n{\n\u00a0\u00a0\u00a0\u00a0User user = FindByAlias(alias).Result;\n\u00a0\u00a0\u00a0\u00a0IMailFolderMessageRulesCollectionPage rules = await _graphClient.Users[user.Id].MailFolders.Inbox.MessageRules.Request().GetAsync();\n\u00a0\u00a0\u00a0\u00a0List&lt;ResultsItem&gt; items = new List&lt;ResultsItem&gt;();\n\u00a0\u00a0\u00a0\u00a0if (rules?.Count &gt; 0)\n\u00a0\u00a0\u00a0\u00a0{\n\u00a0\u00a0\u00a0\u00a0\tforeach (MessageRule rule in rules)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0   items.Add(new ResultsItem{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Display = rule.DisplayName,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Id = rule.Id\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 });\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0return items;\n}<\/pre>\n<div>Above method uses Microsoft Graph SDK to retrieve the message rules and then populates a List object with the rules collection.<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<div>There are additional details and functionality that you can find in the code sample provided in the repository. The complete set of instructions and code are on GitHub in the\u00a0<a href=\"https:\/\/github.com\/microsoftgraph\/dotnetcore-console-sample\">dotnetcore-console-sample<\/a> repo for you try it yourself.<\/div>\n<div><\/div>\n<div>\n<h2>Try It Out<\/h2>\n<p>Navigate to the\u00a0<a href=\"https:\/\/github.com\/microsoftgraph\/dotnetcore-console-sample\">dotnetcore-console-sample<\/a> repo.\u00a0 Do one (or both) of the following:<\/p>\n<p><a href=\"https:\/\/github.com\/microsoftgraph\/30DaysMSGraph-TryItOut\/blob\/master\/Day18-Mailbox.md\">Day 18 repo link<\/a><\/p>\n<ol>\n<li>Clone the repo and configure the project in the Day 18 subfolder.<\/li>\n<li>Follow the instructions in Day 18 to build the project from scratch yourself.<\/li>\n<\/ol>\n<p>If you run into any issues while building or configuring the project please create a new Issue on the repo.<\/p>\n<p>Join us tomorrow as we continue the onboarding steps for the user within an Office 365 tenant using Microsoft Graph requests in <a href=\"https:\/\/developer.microsoft.com\/en-us\/graph\/blogs\/30daysmsgraph-day-19-use-case-assign-permissions-using-unified-groups\/\">Day 19<\/a>.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In preparation for onboarding a user, once the user is created and appropriate license is applied, now we can update the user specific mailbox settings like the users time zone, locale info, working hours etc.<\/p>\n","protected":false},"author":69213,"featured_media":25159,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3],"tags":[84,12],"class_list":["post-2151","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-microsoft-graph","tag-30daysmsgraph","tag-outlook"],"acf":[],"blog_post_summary":"<p>In preparation for onboarding a user, once the user is created and appropriate license is applied, now we can update the user specific mailbox settings like the users time zone, locale info, working hours etc.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts\/2151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/users\/69213"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/comments?post=2151"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts\/2151\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/media\/25159"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/media?parent=2151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/categories?post=2151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/tags?post=2151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}