Ephedra
Ephedra

Reputation: 881

EWS how do I create a Searchfolder in my own mailbox?

I use following Code to Create a SearchFolder but as it gets to the "Save" line it throws following error:

The email address associated with a folder Id does not match the mailbox you are operating on.

private SearchFolder CreateSearchFolder( string email, SearchFilter filter)
{
    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
    service.Credentials = new WebCredentials("mailboxworker", "password");
    service.AutodiscoverUrl(email);

    FolderId folderId = new FolderId(WellKnownFolderName.Inbox, new Mailbox(email));

    FolderId searchFolderId = new FolderId(WellKnownFolderName.SearchFolders, new Mailbox(email));

    // Create the folder.
    SearchFolder searchFolder = new SearchFolder(service);
    searchFolder.DisplayName = "Folder of " + email;

    searchFolder.SearchParameters.SearchFilter = filter;
    // Set the folder to search.
    searchFolder.SearchParameters.RootFolderIds.Add(folderId);   

    // Set the search traversal. Deep will search all subfolders.
    searchFolder.SearchParameters.Traversal = SearchFolderTraversal.Deep;
    // Call Save to make the EWS call to create the folder.
    searchFolder.Save(searchFolderId);

    return searchFolder;
}

What am I doing wrong?

Upvotes: 0

Views: 446

Answers (1)

Jeremy Thompson
Jeremy Thompson

Reputation: 65534

associated with a folder Id does not match the mailbox

All the times I've bumped into this I've fixed it using the Microsoft.Exchange.WebServices.Data WellKnownFolderName enum instead of a string folderId

Here is a working example from MSDN: Create a search folder by using the EWS Managed API

This example assumes that the ExchangeService object has been initialized with valid values in the Credentials and Url properties.

using Microsoft.Exchange.WebServices.Data;

static void CreateSearchFolder(string email)
{
    // Create the folder.
    SearchFolder searchFolder = new SearchFolder(service);
    searchFolder.DisplayName = "From Developer";
    // Create a search filter to express the criteria for the folder.
    EmailAddress developer= new EmailAddress("[email protected]");
    SearchFilter.IsEqualTo fromManagerFilter = new SearchFilter.IsEqualTo(EmailMessageSchema.Sender, developer);

    // Set the search filter.
    searchFolder.SearchParameters.SearchFilter = fromManagerFilter;
    // Set the folder to search.
    searchFolder.SearchParameters.RootFolderIds.Add(WellKnownFolderName.Inbox);
    // Set the search traversal. Deep will search all subfolders.
    searchFolder.SearchParameters.Traversal = SearchFolderTraversal.Deep;
    // Call Save to make the EWS call to create the folder.
    searchFolder.Save(WellKnownFolderName.SearchFolders);
}

Here is another example on MSDN Creating search folders by using the EWS Managed API 2.0

Upvotes: 1

Related Questions