Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find if Folder exists in Inbox and create if not exists

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?

like image 652
greektreat Avatar asked Jun 17 '11 15:06

greektreat


1 Answers

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;
}
like image 92
greektreat Avatar answered Nov 02 '22 17:11

greektreat