9

I am using the Microsoft.Graph nuget package (version 1.6.2) and I try to mark an email as read. This is the code:

        msg.IsRead = true;
        await MainPage.GraphServiceClient.Me.Messages[msg.Id].Request().Select("IsRead").UpdateAsync(msg);

From my understanding, the above code should only send the IsRead property to the server for update. However, executing this, sends the whole message object (I have verified this with Fiddler) and you get this response:

{
  "error": {
    "code": "ErrorInvalidPropertyUpdateSentMessage",
    "message": "Update operation is invalid for property of a sent message.",
    "innerError": {
      "request-id": "<A guid here that I wont share>",
      "date": "2017-07-29T21:10:18"
    }
  }
}

Is the code correct, is this a bug, am I missing something or what? Is there a way around this?

NoOne
  • 3,851
  • 1
  • 40
  • 47

2 Answers2

20

I've just encountered your question after being stuck with the same problem my self.

As I came to see after several tries the problem occurs when you try to update (PATCH) fields that aren't allowed to be updated.

In your case and in my case as well, The problem was that we sent the msg object with all of his fields and not only with the patched and modified fields.

Instead of Doing the following:

var msg = await MainPage.GraphServiceClient.Me
    .Messages[imapMessage.MessageId]
    .Request()
    .GetAsync();

msg.IsRead = true;

await MainPage.GraphServiceClient.Me
    .Messages[msg.Id]
    .Request()
    .Select("IsRead")
    .UpdateAsync(msg);

You should only get the IsRead property in the first place,

Update since version 1.9 of the GraphAPI, we need to send only the updated properties without the readonly properties.

await MainPage.GraphServiceClient.Me
    .Messages[msg.Id]
    .Request()
    .Select("IsRead")
    .UpdateAsync(new Message {IsRead = true});

Hoping it will help you if it's still releavent any way.

Good Night Nerd Pride
  • 8,245
  • 4
  • 49
  • 65
H. Bloch
  • 448
  • 5
  • 12
  • 1
    Awesome! Just tried it and it works! Thanks. :) I haven't though of doing that. – NoOne Nov 08 '17 at 21:33
  • 2
    Note that in version 1.9.0 this code might fail because of this bug: https://github.com/microsoftgraph/msgraph-sdk-dotnet/issues/270. The workaround suggested in that link is also an improvement on the code, since it reduces the size of the request being sent. – NoOne May 19 '18 at 16:50
  • What's the purpose of `Select("IsRead")` is your last example? This [example](https://learn.microsoft.com/en-us/graph/api/message-update?view=graph-rest-1.0&tabs=csharp#example) doesn't use it. – Good Night Nerd Pride Oct 21 '21 at 08:17
  • I tested it and `Select("IsRead")` is not needed. – Good Night Nerd Pride Oct 21 '21 at 09:10
  • It been a long time since i wrote this, it was basically a modification of the original code. it's not needed for sure of you create a new Message object, in case you don't (which is a problem by itself) you have to select, so it won't overwrite the entire object. – H. Bloch Feb 21 '22 at 14:10
2

I found the following code works and wanted to share it as it might help other.

First thing I did was to ensure that the Credentials are set followed by calling the API that sets IsRead = true

 // setup the credentials
 // the brackets are are not used but only to help the data stand-out
 var credentials = new ClientSecretCredential(
     [Your.TenantId],
     [Your.ClientId],
     [Your.ClientValue],
     new TokenCredentialOptions { AuthorityHost = 
           AzureAuthorityHosts.AzurePublicCloud });

 // mark the message as read, 
 // the brackets for .Users[...] are required
 // the brackets for .Messages[...] are also required
 GraphServiceClient graphServiceClient = new GraphServiceClient(credentials);

   await graphServiceClient
      .Users[Your.ApiUserEmail]
      .Messages[Your.Message.id]
      .Request()
      .UpdateAsync(new Microsoft.Graph.Message() { IsRead = true });

if you are attempting the graphServiceClient code without using an "async" method, then use the following:

   graphServiceClient
      .Users[Your.ApiUserEmail]
      .Messages[Your.Message.id]
      .Request()
      .UpdateAsync(new Microsoft.Graph.Message() { IsRead = true }).Result;

Hope this helps...

Art Scott
  • 21
  • 1
  • 1
    need to enable Mail.ReadWrite application permissions and grant admin consent in azure portal for this to work – adumred Apr 12 '23 at 04:41