GraphClient updatemessage for existing message PATCH request contains complete document

Hi Team,

I want to update an incoming email item using the Graphclient, for example, set the categories. The problem is when a change the categories and do an updatemessage the complete message is added to the body of the PATCH request. This results in an error telling that this is not a draft message. I should be able to send a patch message with only the changed properties.

A work-around would be to call the patch request via an httpClient, but there I bumped into the issue that the message.ItemId is a base64 encoded wrapper around the actual messageid. So is there a way to convert the message.ItemId to the messageid for a call to graph API ?

...
var message = client.FetchMessage(messageInfo.ItemId);
message.Categories = new[] { "Processed" };
client.UpdateMessage(message); // throws an exception ... not a draft...

// try update using API call direct ?
var messageId = ConvertToMessageId(message.ItemId);
var url = $"https://graph.microsoft.com/v1.0/users/{mailbox}/mailFolders/{mailfolder}/messages/{messageId}";
...

@accounts.recomatics

To update an incoming email item using the GraphClient and set the categories without sending the complete message in the PATCH request, you can utilize the IGraphClient interface. This interface allows you to interact with the Graph API effectively.

Here’s a general approach you can follow:

  1. Fetch the Message: Use the FetchMessage method to retrieve the message you want to update.
  2. Update the Categories: Modify the Categories property of the message object.
  3. Use the Update Method: Call the UpdateMessage method to apply the changes.

However, if you encounter an issue where the complete message is included in the PATCH request, resulting in an error, you might need to consider using a direct HTTP client call instead.

To convert the ItemId to the actual message ID for the Graph API call, you can implement a conversion function. Unfortunately, the specifics of this conversion are not detailed in the available resources, but you can typically decode the base64 encoded ItemId to retrieve the original message ID.

Here’s a simplified code example to illustrate the process:

var message = client.FetchMessage(messageInfo.ItemId);
message.Categories = new[] { "Processed" };

// Attempt to update using GraphClient
try
{
    client.UpdateMessage(message);
}
catch (Exception ex)
{
    // Handle the exception if the message is not a draft
    Console.WriteLine(ex.Message);
}

// If needed, convert ItemId to messageId for direct API call
var messageId = ConvertToMessageId(message.ItemId);
var url = $"https://graph.microsoft.com/v1.0/users/{mailbox}/mailFolders/{mailfolder}/messages/{messageId}";

// Use HttpClient to send a PATCH request with the updated categories

Please note that the ability to update the message category may depend on the server’s handling of the message and its relationship with the Graph API. If you find that updating the category directly is not possible, you may need to explore alternative methods or consult the API documentation for further guidance.

For more details, you can refer to the IGraphClient Interface for additional functionalities and methods available.

Sources:
[1]: Updating the message category in Outlook

@accounts.recomatics
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): EMAILNET-41449

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Hi @accounts.recomatics
We will investigate the issue with updating the message using Graph client. As a workaround, you can convert ItemId to Id using the code below.

        private static string ConvertToMessageId(string ItemId)
        {
            byte[] bUri = Convert.FromBase64String(ItemId);
            string sUri = Encoding.UTF8.GetString(bUri);
            string[] parts = sUri.Split(':');
            if (parts.Length < 6)
                throw new InvalidOperationException("invalid length");
            int idLength = Convert.ToInt32(parts[1]);
            int headerLength =
                parts[0].Length +
                parts[1].Length +
                parts[2].Length +
                parts[3].Length +
                parts[4].Length +
                parts[5].Length +
                6;

            string id = sUri.Substring(headerLength, idLength);
            return id;
        }
1 Like