Search code examples
javamicrosoft-graph-api

How to get the mails from outlook based on categorized


I need to get all mails from outlook using Graphapi, for below code shows only unread mails will get,

but, here I need to get it category, like below reference

String filterCondition = "isRead eq false";
if (isArchiveFlow) {
    filterCondition = isInfoMailBox ? "" :"isRead eq true";
}
        
return _appClient.users(o365Mailbox)
        .mailFolders("inbox")
        .messages()
        .buildRequest()                
        .select("from,isRead,receivedDateTime,sentDateTime,subject,body,bodyPreview,flag,toRecipients,ccRecipients,bccRecipients,sender,hasAttachments")
        .filter(filterCondition)              
        .top(1000)//Default is 10 and $top accepts a minimum value of 1 and a maximum value of 999 (inclusive).
        .orderBy("receivedDateTime ASC")//ASC - For older emails and DESC - For new emails
        .get();

instead of using isRead conditions I need to get mails based on category.


Solution

  • categories property is a collection, so you need to use any

    String filterCondition = "categories/any(c:c eq 'Green')";
    

    If some messages have two categories, let's say Green and Red and you want to get messages with both categories then filterCondition will be

    String filterCondition = "categories/any(c:c eq 'Green') and categories/any(c:c eq 'Red')";
    

    If want to get messages with either Green or Red

    String filterCondition = "categories/any(c:c eq 'Green') or categories/any(c:c eq 'Red')";