1

I use Exchange Web Services (EWS) from Microsoft.

My code is like this:

ExchangeService server = new ExchangeService(ExchangeVersion.Exchange2007_SP1);

etc.

I get the folders using this code:

Folder rootfolder = Folder.Bind(server, WellKnownFolderName.MsgFolderRoot);

The structure of my folders on Exchange Server is:

Inbox
|
|
Folder1
|
|_Sub_Folder1
| |
| |_Sub_Sub_Folder1
|   |
|   |_Sub_Sub_Sub_Folder1
|
|_Sub_Folder2
| |
| |_Sub_Sub_Folder1
|
Folder2
|
|_Sub_Folder1
|
|_Sub_Folder2

When I use function which is presented above, I receive only the major folders :

Inbox, Folder1 and Folder2.

I would like to receive SubFolders... as well and next I would like to get items from SubFolders.

For Example:

Folder1
|
|_Sub_Folder1
| |
| |_Sub_Sub_Folder1
|   |
|   |_Sub_Sub_Sub_Folder1 -> I want to get these items

Could you help me and provide me an example of some code? What function should I use?

I tried to use:

FindFoldersResults findFoldersResults2 = folder.FindFolders(new FolderView(100));

But when I receive for example folders: Inbox, Folder1, Folder2 and I read ChildFolderCount, I don't receive number of subfolders ex. 2 -> I receive count 0.

What is wrong? I need to read these items from subfolders.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user1492027
  • 11
  • 1
  • 10
  • 2
    I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Feb 15 '13 at 03:49
  • 1
    http://stackoverflow.com/questions/7590510/find-all-subfolders-of-the-inbox-folder-using-ews –  Mar 15 '13 at 09:58

2 Answers2

0

I think you have 2 questions. The first one: "how to get subfolders" and second one "Why is ChildFolderCoun = 0".

The answer to the second one is pretty easy. You have to call folder.Load() and provide a propertyset which is including the FolderSchema.ChildFolderCount propertydefinition.

The answer to your first question is, that you will have to call FindFolders() for each folder in a recusive loop. At least I didn't find a param or something like that which tell's the FindFolders-Method to include all subfolders.

Hope that helps you...

Jürgen Hoffmann
  • 947
  • 4
  • 15
0

You are looking for method to validate if folder exist or not. You can use below method. It recursively searches each subfolders. Change the FolderView's Traversal property for deep / shallow search.

FolderId subfolderInfo;
// Call Method
bool folderExist = isFolderExist(exchange, "Folder1", out subfolderInfo);

//Implementation 

private bool isFolderExist(ExchangeService exchange, string subFolder, out FolderId subfolderInfo)
        {
            try
            {
                FolderView view = new FolderView(100);
                view.PropertySet = new PropertySet(BasePropertySet.IdOnly);
                view.PropertySet.Add(FolderSchema.DisplayName);
                view.Traversal = FolderTraversal.Deep;
                FindFoldersResults findFolderResults = exchange.FindFolders(WellKnownFolderName.Root, view);
                foreach (Folder folder in findFolderResults)
                {
                    if (folder.DisplayName == subFolder)
                    {                        
                        subfolderInfo = folder.Id;
                        return true;
                    }
                }
            }
            catch (Exception Ex)
            {
               ...
            }          
            subfolderInfo = null;
            return false;
        }
kumar chandraketu
  • 2,232
  • 2
  • 20
  • 25