The following code is trying to list all the email folders for a specified user and then all the hierarchical childfolders for that folder.
The following code does not list all the folders when the number of child folders exceeds 10 (even though ChildFolderCount indicates there are more than 10) because the NextPageRequest is always null (see where I have commented // always NULL !!!!)
public async Task<IMailboxes> ListMailboxes(string reference, string wildcardedMailbox, string find)
{
try
{
var options = new List<QueryOption>
{
new QueryOption("$filter", $"mail eq '{_emailAddress}'")
};
var user = await _service.Users.Request(options).GetAsync();
if (user.Count != 1)
throw new Exception($"Unable to find user with email address '{_emailAddress}'");
var userA = _service.Users[user[0].Id];
var folders = await userA.MailFolders.Request().Expand(e => new {e.ChildFolders}).GetAsync();
if (folders == null)
throw new Exception($"Unable to find mailboxes for user with email address '{_emailAddress}'");
_folders.Clear();
var folderHierarchy = new List<string>();
bool more;
do
{
foreach (var folder in folders)
{
var hierarchicalName = $"{folder.DisplayName}";
if (_folders.ContainsKey(hierarchicalName))
{
if (CompoundLogger.Logging) CompoundLogger.LogMessage($"The hierarchical name '{hierarchicalName}' already exists in the dictionary!");
}
else
{
folderHierarchy.Add(hierarchicalName);
_folders.Add(hierarchicalName, folder);
}
if (find == null || find.StartsWith(hierarchicalName))
{
if (folder.ChildFolderCount > 0)
{
folderHierarchy.AddRange(await ListChildren($"{folder.DisplayName}", folder, find, userA));
}
}
}
more = folders.NextPageRequest != null;
if (folders.NextPageRequest != null)
{
folders = await folders.NextPageRequest.GetAsync();
}
} while (more);
return new Mailboxes(folderHierarchy);
}
catch (Exception e)
{
if (e.InnerException is ServiceException exception)
{
LastErrorText = $"Unable to list mailboxes for user {reference}' {Environment.NewLine} {(e.InnerException != null ? exception.Message : e.Message)} {Environment.NewLine} {exception.RawResponseBody}";
}
else
{
LastErrorText = $"Unable to list mailboxes for user {reference}' {Environment.NewLine} : {(e.InnerException != null ? e.InnerException.Message : e.Message)}";
}
throw new Exception(LastErrorText, e);
}
}
private async Task<string[]> ListChildren(string parentDisplayName, MailFolder folder, string find, IUserRequestBuilder userA)
{
if (CompoundLogger.Logging) CompoundLogger.LogMethod(MethodBase.GetCurrentMethod(), new object[] { parentDisplayName, folder?.DisplayName });
var folders = new List<string>();
if (folder == null || folder.ChildFolderCount == 0 || folder.ChildFolders == null)
return folders.ToArray();
bool more = false;
do
{
var results = folder.ChildFolders.CurrentPage;
foreach (var f in results)
{
var hierarchicalName = $"{parentDisplayName}/{f.DisplayName}";
folders.Add(hierarchicalName);
_folders?.Add(hierarchicalName, f);
if (find == null || find.StartsWith(hierarchicalName))
{
if (f.ChildFolderCount > 0)
folders.AddRange(await ListChildren(hierarchicalName, f, find, userA));
}
}
// always NULL !!!!
more = folder.ChildFolders.NextPageRequest != null;
if (folder.ChildFolders.NextPageRequest != null)
{
folder.ChildFolders = await folder.ChildFolders.NextPageRequest.GetAsync();
}
} while (more);
return folders.ToArray();
}
I have found an alternative way to do it as shown below but I am curious as to why the above does not work. I have just started to use the graph api sdk and am just trying to learn
// this alternative method does work
private async Task<string[]> ListChildren(string parentDisplayName, MailFolder folder, string find, IUserRequestBuilder userA)
{
if (CompoundLogger.Logging) CompoundLogger.LogMethod(MethodBase.GetCurrentMethod(), new object[] { parentDisplayName, folder?.DisplayName });
var folders = new List<string>();
if (folder == null || folder.ChildFolderCount == 0)
return folders.ToArray();
var childFolders = await userA.MailFolders[folder.Id].ChildFolders.Request().GetAsync();
bool more;
do
{
foreach (var f in childFolders)
{
var hierarchicalName = $"{parentDisplayName}/{f.DisplayName}";
folders.Add(hierarchicalName);
_folders?.Add(hierarchicalName, f);
if (find == null || find.StartsWith(hierarchicalName))
{
if (f.ChildFolderCount > 0)
folders.AddRange(await ListChildren(hierarchicalName, f, find, userA));
}
}
more = childFolders.NextPageRequest != null;
if (childFolders.NextPageRequest != null)
{
childFolders = await childFolders.NextPageRequest.GetAsync();
}
} while (more);
return folders.ToArray();
}