Created
April 20, 2020 15:40
-
-
Save jasonjoh/f4d9ec4c9eae01c23bad12eadf409088 to your computer and use it in GitHub Desktop.
How to update mailbox settings with .NET Graph SDK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var userId = "some-id"; | |
var mailboxSettings = new MailboxSettings | |
{ | |
AutomaticRepliesSetting = new AutomaticRepliesSetting | |
{ | |
Status = AutomaticRepliesStatus.AlwaysEnabled, | |
InternalReplyMessage = "Hi", | |
ExternalReplyMessage = "Bye" | |
} | |
}; | |
try | |
{ | |
// Get the request URL to the user from the SDK | |
// and add the /mailboxsettings segment | |
var requestUrl = $"{appOnlyClient.Users[userId].Request().RequestUrl}/mailboxsettings"; | |
// Use the SDK's serializer to generate the JSON payload | |
var jsonPayload = appOnlyClient.HttpProvider.Serializer | |
.SerializeAsJsonContent(mailboxSettings); | |
// Create a new Http request message with the JSON payload | |
var requestMessage = new HttpRequestMessage(HttpMethod.Patch, requestUrl); | |
requestMessage.Content = jsonPayload; | |
// Authenticate the message | |
await appOnlyClient.AuthenticationProvider.AuthenticateRequestAsync(requestMessage); | |
// Send the request | |
var response = await appOnlyClient.HttpProvider.SendAsync(requestMessage); | |
if (response.IsSuccessStatusCode) | |
{ | |
Console.WriteLine("Updated mailbox settings"); | |
// If needed, deserialize the response body | |
var responseBody = await response.Content.ReadAsStringAsync(); | |
var responseObject = appOnlyClient.HttpProvider.Serializer | |
.DeserializeObject<MailboxSettings>(responseBody); | |
} | |
else | |
{ | |
throw new ServiceException( | |
new Error | |
{ | |
Code = response.StatusCode.ToString(), | |
Message = await response.Content.ReadAsStringAsync() | |
} | |
); | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine($"Error: {ex.Message}"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment