NextPageRequest is null when trying to list a childfolders for a folder

geetarman 26 Reputation points
2022-06-08T09:29:47.38+00:00

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();  
    }  
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,652 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Zehui Yao_MSFT 5,831 Reputation points
    2022-06-08T10:20:51.073+00:00

    Hello @geetarman , The PageIterator class is designed in the Microsoft Graph SDK to handle enumerating the current page and automatically requesting subsequent pages,
    so only 10 results are returned by default.
    Here is the documentation for your reference: https://learn.microsoft.com/en-us/graph/sdks/paging?tabs=typeScript
    And similar issues that have been resolved: https://learn.microsoft.com/en-us/answers/questions/849587/contactsrequest-not-getting-all-contacts.html
    Hope it can help.
    Best Regards.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.