简体   繁体   中英

Exchange Web Service FolderId for a folder created by user

I have a folder in an Exchange mailbox that is a child of the root and is created by user.

How do I find such a folder using EWS managed API?

I tried using deep traversal, but I can't find the folder.

Edit: Here is the code I am using to get the folder created by user:

ExchangeService server = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
server.UseDefaultCredentials = true;
string configUrl = @"https://yourServerAddress.asmx";
server.Url = new Uri(configUrl);

// set View
FolderView view = new FolderView(100);
view.PropertySet = new PropertySet(BasePropertySet.IdOnly);
view.PropertySet.Add(FolderSchema.DisplayName);
view.Traversal = FolderTraversal.Deep;

FindFoldersResults findFolderResults = server.FindFolders(WellKnownFolderName.Root, view);

// find specific folder
foreach (Folder f in findFolderResults)
{
    // show FolderId of the folder "test"
    if (f.DisplayName == "Test")
    {
        Console.WriteLine(f.Id);
    }
}

You should include the code you're using in your question as you probably just have bug in that. What I do is use a function to find the folder from a string path then you can just call that like GetFolderFromPath(service, "mailbox@domain.com", "\\\\\\folder1\\Folder2") , eg:

internal static Folder GetFolderFromPath(ExchangeService service, String MailboxName, String FolderPath)
{
    FolderId folderid = new FolderId(WellKnownFolderName.MsgFolderRoot, MailboxName);
    Folder tfTargetFolder = Folder.Bind(service,folderid);
    PropertySet psPropset = new PropertySet(BasePropertySet.FirstClassProperties);
    String[] fldArray = FolderPath.Split('\\');

    for (Int32 lint = 1; lint < fldArray.Length; lint++)
    {
        FolderView fvFolderView = new FolderView(1);
        fvFolderView.PropertySet = psPropset;
        SearchFilter SfSearchFilter = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, fldArray[lint]);
        FindFoldersResults findFolderResults = service.FindFolders(tfTargetFolder.Id, SfSearchFilter, fvFolderView);

        if (findFolderResults.TotalCount > 0)
        {
            foreach(Folder folder in findFolderResults.Folders)
            {
                tfTargetFolder = folder;
            }
        }
        else
        {
            tfTargetFolder = null;
            break;
        }
    }
    if (tfTargetFolder != null)
    {
        return tfTargetFolder;
    }
    else
    {
        throw new Exception("Folder Not found");
    }
}

Apparently, your code is correct but it will not give you access to the exchange severer. I have faced the same issue and fixed these lines only.

Actually MS ignores the UseDefaultCredentials = true;

That may be the reason you're not logged into the exchange server. See the MS documentation here for detail.

Use this approach:

var exchange = new MSEWS.ExchangeService(MSEWS.ExchangeVersion.Exchange2007_SP1);
// userid, password, and your network domain
exchange.Credentials = new MSEWS.WebCredentials(userName, password, domain);

exchange.AutodiscoverUrl("tushar.kapoor@bollywood.com");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM