none
How to check if a specific folderpath exists in public folders? RRS feed

  • Question

  • Hi,

    Since several days, I try to find the best way to check if a specific folderpath exists. I' m using vs 2010, c# language and outlook 2010.

    I know I could use foreach loop like that, but the problem is if I add several subfolders in each folders, the're infinite loop . Need some help, thanks

      Outlook.MAPIFolder publicFolders = (Outlook.MAPIFolder)Globals.ThisAddIn.Application.Session.GetDefaultFolder (Outlook.OlDefaultFolders.olPublicFoldersAllPublicFolders);
                
                 foreach (Folder subfolder in publicFolders .Folders)
                 {
      if (subfolder.FolderPath == PathToCheck)
        {
                        return true;
        }   
                  
                   else
                      return false;

                     foreach (Folder subfolder1 in subfolder.Folders)
                     {
        if (subfolder.FolderPath == PathToCheck)
        {
                        return true;
        }   
                  
                      else
                        return false;
       }
       ...
      }

    Friday, September 13, 2013 1:08 PM

Answers

  • Try this, I cleaned up Dmitry's off the cuff code:

    string[] folderNames = pathToCheck.Split('\\');
    Folders folders = publicFolders.Folders;
    for(jnt i = 1; i <= folderNames.length; i++)
    {
     
    MAPIFolder folder = folders[folderNames[i]];
     
    if (folder == null) return null;
     
    if (i == folderNames.length) return folder;
      folders
    = folder.Folders;
    }


    Ken Slovak MVP - Outlook

    Tuesday, September 17, 2013 6:16 PM
  • I used this mehod GetFolder and it seems to work

    public Outlook.Folder GetFolder(string folderPath)
            {
                Outlook.Folder returnFolder = null;

                try
                {
                    // Remove leading "\" characters.
                    folderPath = folderPath.TrimStart ("\\" . ToCharArray());

                    // Split the folder path into individual folder names.
                    String[] folders = folderPath.Split ("\\" . ToCharArray());

                    // Retrieve a reference to the root folder.
                    returnFolder = Globals.ThisAddIn.Application.Session.Folders[folders[0]] as Outlook.Folder;

                    // If the root folder exists, look in subfolders.
                    if (returnFolder != null)
                    {
                        Outlook.Folders subFolders = null;
                        String folderName;

                        // Look through folder names, skipping the first
                        // folder, which you already retrieved.
                        for (int i = 1; i < folders.Length; i++)
                        {
                            folderName = folders[i];
                            subFolders = returnFolder.Folders;
                            returnFolder = subFolders[folderName] as Outlook.Folder;
                        }
                    }
                }
                catch
                {
                    returnFolder = null;
                }

                return returnFolder;
            }

    Thursday, September 19, 2013 1:39 PM

All replies

  • Split the path into an array of strings and recursively process the folders starting with the top level folder.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, September 13, 2013 4:10 PM
  • Hi Dimitri, Thanks for your reply, could you explain more with the code bellow?
    Monday, September 16, 2013 10:57 AM
  • Off the top of my head:

    string[] folderNames = pathToCheck.Split('\\');
    MAPIFolders folders = publicFolders.Folders;
    for(jnt i = 0; i < folderNames.length; i++)
    {
      MAPIFolder folder = folders.Item(folderNames[i]);
      if (folder == null) return null;
      if (i == folderNames.length-1) return folder;
      folders = folder.Folders;
    }


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Monday, September 16, 2013 1:49 PM
  • Thanks for your answer, I found another solution:

    Firstly,  I enumerate all folders in public folders, or another top level

    Then, I compare my path to all folder's path in public folder

    public void EnumerateFolders(Outlook.Folder folder)
            {
                Outlook.Folders childFolders = folder.Folders;
                if (childFolders.Count > 0)
                {
                    foreach (Outlook.Folder childFolder in childFolders)
                    {
                        arrText1.Add(childFolder);
                        EnumerateFolders(childFolder);
                    }
                }
            }   

    after that I use a foreach to make the comparison

    Monday, September 16, 2013 3:23 PM
  • Don't do that. If you have thousands of folders in the PF store, it will take ages to run your code.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Monday, September 16, 2013 3:52 PM
  • I tried to use your code, but there are many error:

    As far as I'm concerned, it's MAPIFolder not MAPIFolders, may be there is this kind of type, but I don't know, I'm begginer. And I couldn't make an equivalent between mapiFolder and publicFolders.Folders, it's 2 different type

    And in the for loop, I guess it's folders.Items not folders.Item 'cause this last one doesn't exist.

    I coudn't use Items as a method.

    Tuesday, September 17, 2013 7:18 AM
  • Try this, I cleaned up Dmitry's off the cuff code:

    string[] folderNames = pathToCheck.Split('\\');
    Folders folders = publicFolders.Folders;
    for(jnt i = 1; i <= folderNames.length; i++)
    {
     
    MAPIFolder folder = folders[folderNames[i]];
     
    if (folder == null) return null;
     
    if (i == folderNames.length) return folder;
      folders
    = folder.Folders;
    }


    Ken Slovak MVP - Outlook

    Tuesday, September 17, 2013 6:16 PM
  • Hi, thanks for this code. I think it works for checking folders in public folder which path looks like

    \\ P F \all PF\group

    But what about path of folder's subfolders

    Wednesday, September 18, 2013 8:25 AM
  • What about the subfolders? What happens when you step through the code?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, September 18, 2013 1:16 PM
  • With this cleaned code, I use try and catch displaying folder.Name in a MessageBox. I've twice Message error "The attempted operation failed, An object could not found " , once "Group" twice "Object not found" again and finally "Index was out of range, must be non-negative and less than the size of the collection"

    My pathToCheck looks like this: \\ Public folders - username...\ All public folders \ Group \ tracking \ cat 1

    I think the loop compares folders only folders  in public folders  to pathToCheck.Split, not it's subfolders, that's why the first two items and last  items in my array gives back object not found.

    Wednesday, September 18, 2013 2:49 PM
  • Look at your name array, does it show all the split parts of the path to the subfolders?

    Ken Slovak MVP - Outlook

    Wednesday, September 18, 2013 2:55 PM
  • Yes it does, it's displaying : Public folders - username, All public folders, Group, tracking and cat 1

     
    Wednesday, September 18, 2013 3:03 PM
  • And the actual subfolder names are "Group" as a subfolder of all public folders, with "tracking" a subfolder of Group, and "cat 1" a subfolder of tracking?

    If you get a valid reference to all public folders using NameSpace.GetDefaultFolder(olPublicFoldersAllPublicFolders) as say AllPub, does this work to get Group:

    MAPIFolder group = AllPub.Folders["Group"];


    Ken Slovak MVP - Outlook

    Wednesday, September 18, 2013 3:18 PM
  • Yes, clearly I want to check programmaticaly if the pathTocheck matches to folder or subfolder of public folders. in my public folders, I've All public folders, in this folder, there is Group subfolder, in Group it's Tracking subfolder and in tracking, cat 1 subfolder.

    My pathToCheck looks like this : \\ Public Folders - username \ All Public Folders\ Group\ Tracking\ Cat

    When I try to display this in a messageBox, it works, I can get group:

    Outlook._NameSpace oNS = Globals.ThisAddIn.Application.GetNamespace("MAPI");
    Outlook.MAPIFolder AllPub = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olPublicFoldersAllPublicFolders);
                MAPIFolder group = AllPub.Folders["Group"];
    But I want to avoid to get specific folder using  MAPIFolder group = AllPub.Folders["Group"]; 'cause the code won't work if later I would like to change the folderPath which matches to other subfolders in All public folder, like \\ Public Folders - username \ All Public Folders\ Scanner \ ...
    • Edited by suki_11 Thursday, September 19, 2013 6:37 AM
    Thursday, September 19, 2013 6:31 AM
  • I used this mehod GetFolder and it seems to work

    public Outlook.Folder GetFolder(string folderPath)
            {
                Outlook.Folder returnFolder = null;

                try
                {
                    // Remove leading "\" characters.
                    folderPath = folderPath.TrimStart ("\\" . ToCharArray());

                    // Split the folder path into individual folder names.
                    String[] folders = folderPath.Split ("\\" . ToCharArray());

                    // Retrieve a reference to the root folder.
                    returnFolder = Globals.ThisAddIn.Application.Session.Folders[folders[0]] as Outlook.Folder;

                    // If the root folder exists, look in subfolders.
                    if (returnFolder != null)
                    {
                        Outlook.Folders subFolders = null;
                        String folderName;

                        // Look through folder names, skipping the first
                        // folder, which you already retrieved.
                        for (int i = 1; i < folders.Length; i++)
                        {
                            folderName = folders[i];
                            subFolders = returnFolder.Folders;
                            returnFolder = subFolders[folderName] as Outlook.Folder;
                        }
                    }
                }
                catch
                {
                    returnFolder = null;
                }

                return returnFolder;
            }

    Thursday, September 19, 2013 1:39 PM