1

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;
}
CarenRose
  • 1,266
  • 1
  • 12
  • 24
J. A.
  • 15
  • 8
  • What is the error you are getting? – Scrobi Dec 27 '18 at 14:31
  • in addition is this for office365? If so you look at using the Microsoft graph API as an alternative solution...[docs](https://learn.microsoft.com/en-us/graph/api/resources/mailfolder?view=graph-rest-1.0) – Scrobi Dec 27 '18 at 14:53

1 Answers1

0

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

CarenRose
  • 1,266
  • 1
  • 12
  • 24
Alina Li
  • 884
  • 1
  • 6
  • 5