Skip to content

Instantly share code, notes, and snippets.

@jasonjoh
Created April 20, 2020 15:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonjoh/f4d9ec4c9eae01c23bad12eadf409088 to your computer and use it in GitHub Desktop.
Save jasonjoh/f4d9ec4c9eae01c23bad12eadf409088 to your computer and use it in GitHub Desktop.
How to update mailbox settings with .NET Graph SDK
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