1

I am trying to add a Category to an email via Graph. I found this: https://github.com/microsoftgraph/microsoft-graph-docs/blob/main/api-reference/v1.0/resources/outlookcategory.md But it only explains how to create a new category. I already got my categories and I'd like to add one to a mail I've also got in memory.

var categories = await graphService.Users[metadata.UserOid].Outlook.MasterCategories.Request().GetAsync();
var category = categories.SingleOrDefault(c => c.DisplayName == name);
if (category == null)
{
  if (KNOWN_OUTLOOK_CATEGORIES.ContainsKey(name))
  {
    await graphService.Users[metadata.UserOid].Outlook.MasterCategories.Request().AddAsync(KNOWN_OUTLOOK_CATEGORIES[name]);
    if (retry > 3)
    throw new InvalidOperationException($"Cannot create category in mailbox: {name}.");
    await SetCategoryMailByName(name, graphService, metadata, retry + 1);
  }
}
// Add my category to the mail
await graphService.Users[metadata.UserOid].Messages[m.MailId].Categories.Add(category);

Problem here is that there is no category in the mail. How could I add one?

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
greg-e
  • 374
  • 4
  • 18

1 Answers1

0

Try adding a new Message object with the new category instead of trying to add it to the message categories list:

var message = new Message
{
    Categories = ["new category"]
};


await graphClient.Me.Messages["{message-id}"]
    .Request()
    .UpdateAsync(message);
m b k
  • 145
  • 1
  • 4