How to get public folders from one Public folder mailbox in Exchange 2013 via EWS

柔情痞子 提交于 2019-12-25 06:55:44

问题


I can retrieve public folders stored in specific Public folder mailbox using this powershell command:

Get-PublicFolder –GetChildren | Where { $ _.ContentMailboxName –eq “PFMailbox1” }

(But I don't want to use remote PowerShell)

I'm not able to do this using EWS.

My first idea was to get all public folders and then sort them according Public folder mailboxes.

But there is probably no Extended MAPI property which contains public folder mailbox name (similar to ContentMailboxName powershell property).

So I tried this: EWS with delegate access

var mailbox = new Mailbox("PFMailbox1@MyDomain.local"); 
// PFMailbox1 is Public Folder mailbox with Pubclic folders
FolderId folderId = new FolderId(WellKnownFolderName.MsgFolderRoot, mailbox);
Folder rootfolder = Folder.Bind(service, folderId);

(WellKnownFolderName property was tested with .Root and PublicFolderRoot too)

but I always get error:

"The request failed. The remote server returned an error: (503) Server Unavailable." or "An unhandled exception of type 'Microsoft.Exchange.WebServices.Data.ServiceResponseException' occurred in Microsoft.Exchange.WebServices.dll"

When I tried impersonation

service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, impUser);
// impUser=PFMailbox1@MyDomain.local
Folder rootfolder = Folder.Bind(service, WellKnownFolderName.MsgFolderRoot );

I get error:

"The account does not have permission to impersonate the requested user."

All mailbox permission for user Administrator and PFMailbox1 are set to full access. I'm using latest Exchange2013 dll's.

EDIT1:

Second issue is how to create root public folder and save it to desired public folder mailbox?

EWS method Folder.Save(FolderId) has only one parameter and if I use FolderId = PublicFolderRoot -> all folders will be saved into MasterHierarchy Public Folder Mailbox (mailbox which was first created).

The only solution I know is to create first level (root) folders using Remote Power Shell for every Public Folder Mailbox.

New-PublicFolder "Folder1" -Mailbox "PFMailbox1"
New-PublicFolder "Folder2" -Mailbox "PFMailbox2"

and then on second (third,..) folder level I can use Folder.Save(FolderID). But how to do it using EWS ?


回答1:


I didn't try this with delegate access or impersonation, but if I'm a publishing editor for a public folder, I'm able to retrieve the direct subfolders of the public folder mailbox using the following code. There is a FolderTraversal value to specify a shallow search (direct subfolders only) or a deep search (the entire hierarchy) but you can't use deep on public folders.

private static void GetFolderHierarchy(int folderViewSize, ExchangeService service)
    {
        FolderView view = new FolderView(folderViewSize);
        view.PropertySet = new PropertySet(BasePropertySet.IdOnly, FolderSchema.DisplayName);     

        // Call FindFolders to retrieve the folder hierarchy, starting with the PublicFoldersRoot folder.
        FindFoldersResults findResults = service.FindFolders(WellKnownFolderName.PublicFoldersRoot, view);

        foreach (Folder folder in findResults.Folders)
        {
             Console.WriteLine("Public folder display name: {0} ", folder.DisplayName);

        }
   }



回答2:


I found a possible solution.

There is an unnamed extended EWS property "0x6656" in public folder in Exchange 2013 (I used OutlookSpy - selected public folder mailbox, click on EMAPIFolder).

There is, for example, 4e1f53e4-0f2d-46eb-873f-b4857d9d395a@myDomain.local

The value is the same for every folder in one public folder mailbox. The GUID (before @) is the ExchangeMailboxGuid - this can be read together with public folder mailbox from Active Directory and then pair public folders with mailbox.

The problem is that I was not able to read this property using managed EWS (I'm not able to read any unnamed Extended properties). I used this definition:

var ExchangeMailboxGuid = new ExtendedPropertyDefinition(0x6656, MapiPropertyType.String);

Maybe I can try unmanaged EWS, but it is more complicated.

As a temporary solution I combined EWS and remote PowerShell. Here is my pseudocode:

if (folderId == null)   // It is root public folder
{
    var mailboxGuid = GetMailboxId();
    // public folder root
    var ewsFolderId = EwsAdapter.GetPublicFolderId(folderId);
    // get all root public folders from all public folder mailboxes
    var tempFindFolderResults = FindFolders(ewsFolderId);

    var powerShellConnection = new powerShellConnection(ConnectionConfiguration);
    // get all root public folders with info which mailbox is owner
    var PublicFolderMailboxes = powerShellConnection.GetPublicFolders();

    foreach (var publicFolderMailbox in PublicFolderMailboxes)
    {
        if (publicFolderMailbox.Attributes["ExchangeMailboxGuid"].Value == mailboxGuid)
        {
            foreach (var tempFindFolderResult in tempFindFolderResults)
            {
                if (tempFindFolderResult.DisplayName == publicFolderMailbox.Attributes["Name"].Value)
                {
                    // add only folder from selected public folder mailbox
                    findFolderResults.Add(tempFindFolderResult);
                }
            }
        }
    }
}
else  // it is public subfolder - standard handling
{
    var ewsFolderId = EwsAdapterHelper.GetPublicFolderId(folderId);
    findFolderResults = FindFolders(ewsFolderId);
}

Here's the PowerShell command I use in the method GetPublicFolders:

Get-PublicFolder -GetChildren

Edit: I think EWS doesn't fully support Exchange 2013 Public Folders -

  1. how to get public folders from any "Public Folder Mailbox" and
  2. save public folder in root level to any "Public Folder Mailbox" (Not only Primary Public Folder Mailbox).


来源:https://stackoverflow.com/questions/21911206/how-to-get-public-folders-from-one-public-folder-mailbox-in-exchange-2013-via-ew

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!