If it is possible to search mailFolder using msgraph API by mailFolder displayName

1.2k views Asked by At

I want to implement some functionallity to allow user to search mailFolder using msGraph API

If it is possible to search mailFolder using msgraph API by mailFolder displayName In documentation I didnt find any mentions about how to search mailFolder. It only shows how to get mailFolder by ID. Can we even search mailFolder? Or search is not supported for mailFolders? Thanks for any answers or suggetions

4

There are 4 answers

1
user2250152 On

You can use $filter query parameter and search for a folder the match displayName

GET https://graph.microsoft.com/v1.0/me/mailFolders?$filter=displayName eq 'name'

or if displayName starts with some value

GET https://graph.microsoft.com/v1.0/me/mailFolders?$filter=startswith(displayName,'name')

Resource:

Filter parameter

0
Tekkion On

Get all root folders than loop through them and search for their childfolders. My example only searches the inbox for the childfolder but you should get the idea:

          var folders = await graphClient.Users[user.id].MailFolders.Request().Filter("displayname eq '"+DestFolderName+"'").GetAsync();
                if (folders is null || folders.Count != 1)
                {
                    folders = await graphClient.Users[user.id].MailFolders.Request().Filter("displayname eq 'Posteingang'").GetAsync();
                    if (folders.Count == 1)
                    {
                        var childfolders = await graphClient.Users[user.id].MailFolders[folders.First().Id].ChildFolders.Request().Filter("displayname eq '"+DestFolderName+"'").GetAsync();
                        if (childfolders is null || folders.Count != 1)
                            return BadRequest("Could not find folder!");
                        else
                            folderid= childfolders.First().Id;
                    }
                    else
                        return BadRequest("Could not find folder!");
                }
                else
                    folderid= folders.First().Id;
0
John Ranger On

Yes, this is possible - in a 2 step process.

First, get all mailfolders (Root- and subfolders) of a mailbox:

https://graph.microsoft.com/v1.0/users/<[ID] or UPN of mailbox>/mailFolders/delta

This gives you an array with all mailFolders. In my case I store them in $MailFolders (Powershell)

Secondly, get only the one which has the desired displayName out of the array.

For example, in Powershell, I do this:

$Mailfolder = $MailFolders | where displayName -eq "<displayName of desired folder>"
0
viebrix On

Adapted Tekkion's solution to work recursive and with new SDK API (for example Mailfolders.Request() isn't working here any more)

c#

public GraphServiceClient GraphClient { get; set; }
        
        public async Task<Microsoft.Graph.Models.MailFolder> searchMailFolder(Microsoft.Graph.Models.MailFolder parentFolder, string displayName)
        {
             var subfolders = await GraphClient.Me.MailFolders[parentFolder.Id].ChildFolders.GetAsync((requestConfiguration) =>
            {
                requestConfiguration.QueryParameters.Filter += "displayname eq '" + displayName + "'";
            }
            );
            if (subfolders.Value.Count == 1)
            {
                return subfolders.Value.FirstOrDefault();
            }
            else if (subfolders.Value.Count == 0)
            {
                subfolders = await GraphClient.Me.MailFolders[parentFolder.Id].ChildFolders.GetAsync();
                           foreach(MailFolder folder in subfolders.Value) 
                {
                    var subfolders1 = await searchMailFolder(folder, displayName, user, guiForm);
                    if(subfolders1!= null)
                        return subfolders1;
                }
            }
            return null;
        }

        public async Task<string> searchMailFolder(string displayName)
        {
            var folder = await searchMailFolder(await GraphClient.Me.MailFolders["msgfolderroot"].GetAsync(),displayName);

            return folder.Id;
        }
    

First method searches for a subfolder with the specific displayName. If it isn't found, search all subfolders of this subfolder recursive with the same function. If it is found, return the folder.

Second method starts first method with the rootfolder (msgfolderroot) and returns the id.