Search code examples
c#microsoft-graph-apiexchangewebservices

Get recent conversation message from MS Graph API


I am not able to Get conversation nodes from email message using MS graph API. I need recent item of email trail.

My application used Microsoft.Exchange.WebServices to get email and conversations, Now I am upgrading this application to MS Graph API.

Old Code:

EmailMessage email = EmailMessage.Bind(service, new ItemId(emailId));
ConversationResponse response = service.GetConversationItems(email.ConversationId,
                                              properties,
                                             null,
                                             null,
                                             ConversationSortOrder.TreeOrderAscending); 
foreach (ConversationNode objC in response.ConversationNodes)
 {
       foreach (Item objItem in objC.Items)
       {
            objItem.Load(new PropertySet(EmailMessageSchema.UniqueBody, ItemSchema.Attachments,
                                                    EmailMessageSchema.Sender,
                                                   EmailMessageSchema.Subject));
       }
 }

Graph API:

Message message = await _client.Users[_config.UserName].Messages[emailId]
                      .Request()
                      .GetAsync();

I have not find any property or method to get Conversation Nodes in message.


Solution

  • You can use the ConversationId and a Filter to get all the messages within a conversation eg something like

    var ConversationMessages = await GraphServiceClient.Users[UserId].Messages.Request().Filter("conversationid eq '" + Message.ConversationId + "'").GetAsync();
    

    If you want the messages returned in Last received order you need to also include the receivedDateTime in the Filter then you can order by that (without getting an error) eg

    var ConversationMessages2 = await GraphServiceClient.Users[UserId].Messages.Request().Filter("(receivedDateTime gt 2020-08-20) and (conversationid eq '" + Message.ConversationId + "')").OrderBy("receivedDateTime desc").GetAsync();