7

Just starting to use Exchange Webservices 1.1 on Exchange 2010. I can't find an example on how to find specific folders and if not exists, create it. How is this done?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
greektreat
  • 2,329
  • 3
  • 30
  • 53

1 Answers1

15

Well after a few days of fiddling and research on the net i figured it out:

FolderView fv = new FolderView(10);

var findFoldersResults = service.FindFolders(
    WellKnownFolderName.Inbox,
    new SearchFilter.SearchFilterCollection(
        LogicalOperator.Or,
        new SearchFilter.ContainsSubstring(FolderSchema.DisplayName, "ERROR"), new SearchFilter.ContainsSubstring(FolderSchema.DisplayName, "ARCHIVE")),
    fv);

foreach (var folder in findFoldersResults)
{
    if (folder is Folder)
    {
        if (folder.DisplayName.ToUpper() == "ARCHIVE")
        {
            archiveFolderID = folder.Id;
        }
        else if (folder.DisplayName.ToUpper() == "ERROR")
        {
            errorFolderID = folder.Id;
        }

    }
}
//if archive folder not found create and assign the variable to the folderID
if (archiveFolderID == null)
{
    Folder folder = new Folder(service);
    folder.DisplayName = "ARCHIVE";
    folder.Save(WellKnownFolderName.Inbox);
    archiveFolderID = folder.Id;
}
//if error folder not found create and assign the variable to the folderID
if (errorFolderID == null)
{
    Folder folder = new Folder(service);
    folder.DisplayName = "ERROR";
    folder.Save(WellKnownFolderName.Inbox);
    errorFolderID = folder.Id;
}
greektreat
  • 2,329
  • 3
  • 30
  • 53
  • 3
    probably you could also use SearchFilter.IsEqualTo because ContainsSubstring will return folders with names like "NoERRORS" or "ERRORSNotAllowed" while IsEqualTo uses == oparator so basically you would not have to do your own 'if(folder.DisplayName.ToUpper() == "ERROR")' – grapkulec Jul 20 '11 at 07:57
  • @greektreat How we can do the same for the sub folders – Aaditya R Krishnan May 26 '22 at 08:56
  • @Ark, I think when you use folder.Save(WellKnownFolderName.Inbox) the value of WellKnownFolderName.Inbox is the folderID so if you get the folder ID of the one you want to be as the parent It may work. I haven't touched this since 2011. take a look at this post https://stackoverflow.com/questions/32496752/create-folder-in-other-mailbox-with-ews-api – greektreat May 31 '22 at 20:45