Search code examples
c#visual-studio-2010outlookexchangewebservices

Trying to find unique id of folder in EWS C# API


I am trying to find the unique id of a folder in Outlook.

For some reason I keep getting an error with the AutoDiscoverUrl method, but I have no idea why. I have tried all the possible solutions on StackOverflow.

I am trying to run it in a commandline program using C#, and have commented/documented the code. I have used others' example on how to do this, but it doesn't work.

static void Main(string[] args)
{
    // Set server binding
    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
    service.UseDefaultCredentials = true;

    // Set Credentials
    service.Credentials = new WebCredentials("xxxx", "xxxxx", "xxxx");
    service.UseDefaultCredentials = true;

    service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");

    // Set the URL 
    service.AutodiscoverUrl("xxxx", Callback);

    // Error Tracing
    service.TraceEnabled = true;

    // Redirect callback

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

    FindFoldersResults findFolderResults = service.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);
        }
    }
}

private static bool Callback(string redirectionUrl)
{
    bool result = false;
    var redirectionUri = new Uri(redirectionUrl);
    if (redirectionUri.Scheme == "https")
    {
        result = true;
    }
    return result;
}

Solution

  • You could find the unique id of the folder using the below code:

    ExchangeService Service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
    Service.UseDefaultCredentials = false;
    Service.Credentials = new WebCredentials("yourUserID", "yourPassword");
    
    Mailbox ProdSupportMailbox = new Mailbox("ProdSupport@company.com");
    Service.AutodiscoverUrl("ProdSupport@company.com");
    
    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);
        }
    }
    

    For more information, please refer to these links:

    Exchange Web Service FolderId for a not well known folder name

    Microsoft Exchange Web Services reading my local Outlook folders INSTEAD of another address