locked
Get Folders, Subfolders hierarchy in EWS RRS feed

  • Question

  • Hello,

    I'm using the Microsoft.Exchange.WebServices.Data.ExchangeService to retrieve all folders and subfolders
    but I'm having problem with the hierarchy (parent - child)

    _exchangeService.FindFolders(new FolderId(_folder, _userMailBox), new FolderView(int.MaxValue) { Traversal = FolderTraversal.Deep });
    to retrieve a collection of Folder.

    I've tried to match the id's (Id and ParentFolderId) but they don't match, the only helpful property is ChildFolderCount which give's me the number of child folder but still I cannot do the mapping.

    Thanks in advance

    Wednesday, February 14, 2018 9:48 AM

Answers

  • Ok, it seems I was wrong after all about the Id and ParentFolderId. Sorry :-/
    They do match and there is a hierarchy.

    For the mapping I used this class

    public class FolderObject
    
        {
            publicstring UniqueId { get; set; }
            publicstring UniqueParentId { get; set; }
     
            publicstring DisplayName { get; set; }
            publicint UnreadCount { get; set; }
            publicint ChildFolderCount { get; set; }
            
            publicList<FolderObject> NestedFolders { get; set; } = newList<FolderObject>();
        }
    ////
    @functions {
     
        public List<FolderObject> FlatToHierarchy(List<Folder> list)
        {
            var lookup = new Dictionary<string, FolderObject>();
     
            var nested = new List<FolderObject>();
     
            foreach (var item in list)
            {
                var dto = new FolderObject
                {
                    UniqueId = item.Id.UniqueId,
                    UniqueParentId = item.ParentFolderId.UniqueId,
                    DisplayName = item.DisplayName,
                    UnreadCount = item.UnreadCount,
                    ChildFolderCount = item.ChildFolderCount,
                };
     
                if (lookup.ContainsKey(item.ParentFolderId.UniqueId))
                {
                    lookup[item.ParentFolderId.UniqueId].NestedFolders.Add(dto);
                }
                else
                {
                    nested.Add(dto);
                }
     
                lookup.Add(item.Id.UniqueId, dto);
            }
     
            return nested;
        }
     
    }

      

    Thanks to gregmac for the FlatToHierarchy
    https://stackoverflow.com/questions/319108/building-hierarchy-objects-from-flat-list-of-parent-child




    Wednesday, February 14, 2018 3:39 PM