Skip to main content

Using Office 365 Groups within the EWS Managed API

Office365 Groups (or unified\modern groups) is a new feature the was introduced in Office365 that Exchange Online is an active participant in by providing the Email/Conversation and calendar portions of this feature. Recently there was a new unified REST API released in preview https://msdn.microsoft.com/en-us/office/office365/howto/office-365-unified-api-overview that I'll look at another day but with this post I want to look at just accessing the Exchange portion of the unified Groups using EWS and also we will take a look at how the config is stored in the mailbox.

Accessing using the EWS Managed API

If your following the changes to the EWS Managed API open source repository https://github.com/OfficeDev/ews-managed-api support for reading the available Unified groups a Mailbox is a member of was added in April. This is done using the yet to be documented GetUserUnifiedGroups EWS operation. So if you have a version of the Managed API compiled from this source that includes these updates (there is a sample one from one of my other post here ) . You can then use the following code to get all the Unified Groups a Mailbox is a member of

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
            RequestedUnifiedGroupsSet Group = new RequestedUnifiedGroupsSet();
            Group.FilterType = UnifiedGroupsFilterType.All;
            Group.SortDirection = SortDirection.Ascending;
            Group.SortType = UnifiedGroupsSortType.DisplayName;
            List<RequestedUnifiedGroupsSet> reqG = new List<RequestedUnifiedGroupsSet>();
            reqG.Add(Group);
            Collection<UnifiedGroupsSet> ugGroupSet = service.GetUserUnifiedGroups(reqG,"jcool@domain.com");
            foreach (UnifiedGroupsSet ugset in ugGroupSet)
            {
                foreach (UnifiedGroup ugGroup in ugset.Groups)
                {
                    Console.WriteLine(ugGroup.SMTPAddress);
                }
            }
 

or in PowerShell

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
$Group = New-Object Microsoft.Exchange.WebServices.Data.Groups.RequestedUnifiedGroupsSet
$Group.FilterType = [Microsoft.Exchange.WebServices.Data.Groups.UnifiedGroupsFilterType]::All;
$Group.SortDirection = [Microsoft.Exchange.WebServices.Data.SortDirection]::Ascending;
$Group.SortType = [Microsoft.Exchange.WebServices.Data.Groups.UnifiedGroupsSortType]::DisplayName;
$reqG = New-Object -TypeName 'System.Collections.Generic.List[Microsoft.Exchange.WebServices.Data.Groups.RequestedUnifiedGroupsSet]'
$reqG.Add($Group);
$ugGroupSet = $service.GetUserUnifiedGroups($reqG,"jcool@domain.com");
foreach ($ugset in $ugGroupSet)
{
    foreach ($gGroup in $ugset.Groups)
    {
        $gGroup.SmtpAddress
    }
}

(Note as I'm writing this I think there is a bug in this operation when using the Mailbox overload)

Once you have the SMTPAddress of the Group you can just Bind to the Group using a normal Folder bind eg either the Inbox (for conversations) or the Calendar

1
2
$folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Calendar,$gGroup.SmtpAddress)   
$Calendar = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)

Technical Bit

For those people interested in the how this get implemented in your mailbox here's what it looks like. In the Mailbox Non_IPM_Subtree there is a folder called MailboxAssociations this gets created eg



Then in the normal Messages collection of this folder when you subscribe to a group (or are made a member of one) an item of type IPM.MailboxAssociation.Group will be created. On this Item's extended properties get set with the relevant information for the group eg




So if you have an older version of the Managed API where you don't have access to the newer updates you can still get the Group memberships for a mailbox by accessing these IPM.MailboxAssociation.Group objects and then reading the extended properties. Eg first to bind to the MailboxAssociations use the following extended property which will give you the HexId of the folder which you can then convert to an EWSId to bind to the folder.

1
2
3
4
5
6
7
8
            ExtendedPropertyDefinition MailboxAssociationFolderEntryId = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.Common, "MailboxAssociationFolderEntryId", MapiPropertyType.Binary);
            PropertySet fldPropSet = new PropertySet(BasePropertySet.FirstClassProperties);
            fldPropSet.Add(MailboxAssociationFolderEntryId);
            Folder rtFolder = Folder.Bind(service, new FolderId(WellKnownFolderName.Root,"jcool@datarumble.com"), fldPropSet);
            Byte[] MAFldId = null;
            rtFolder.TryGetProperty(MailboxAssociationFolderEntryId, out MAFldId);
            AlternateId aiEWS = (AlternateId)service.ConvertId(new AlternateId(IdFormat.HexEntryId, BitConverter.ToString(MAFldId).Replace("-", ""), "jcool@datarumble.com"), IdFormat.EwsId);
            Folder MbAssoc = Folder.Bind(service,new FolderId(aiEWS.UniqueId));

Once you bind to the MailboxAssoications Folder you can then just use a FindItem op with a search filter to limit the results to just the Group associations. Then pull the extended property for the LegacyDN of the MailboxAssoication then you just need to resolve that to the SMTPAddress eg

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
            PropertySet itmPropSet = new PropertySet(BasePropertySet.FirstClassProperties);
            ExtendedPropertyDefinition MailboxAssociationExternalId = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.Common, "MailboxAssociationExternalId", MapiPropertyType.String);
            ExtendedPropertyDefinition MailboxAssociationLegacyDN = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.Common, "MailboxAssociationLegacyDN", MapiPropertyType.String);
            ExtendedPropertyDefinition MailboxAssociationIsMember = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.Common, "MailboxAssociationIsMember", MapiPropertyType.Boolean);
            itmPropSet.Add(MailboxAssociationExternalId);
            itmPropSet.Add(MailboxAssociationLegacyDN);
            itmPropSet.Add(MailboxAssociationIsMember);
            SearchFilter sfFilter = new SearchFilter.IsEqualTo(ItemSchema.ItemClass, "IPM.MailboxAssociation.Group");
            ItemView ivMbAsView = new ItemView(1000);
            ivMbAsView.PropertySet = itmPropSet;
            FindItemsResults<Item> GroupAssoications = MbAssoc.FindItems(sfFilter, ivMbAsView);
            List<String> GroupSMTPAddresses = new List<string>();
            foreach (Item itItem in GroupAssoications.Items)
            {
                String mbAssoicatedDn = "";
                bool MailboxAssociationIsMemberValue = false;
                itItem.TryGetProperty(MailboxAssociationIsMember, out MailboxAssociationIsMemberValue);
                if (MailboxAssociationIsMemberValue)
                {
                    if (itItem.TryGetProperty(MailboxAssociationLegacyDN, out mbAssoicatedDn))
                    {
                        NameResolutionCollection ncCol = service.ResolveName(mbAssoicatedDn, ResolveNameSearchLocation.DirectoryOnly, true);
                        if (ncCol.Count > 0)
                        {
                            GroupSMTPAddresses.Add(ncCol[0].Mailbox.Address);
                        }
                    }
                }
            }

So as you can see it's a little more complicated this way so try to use the other op where you can.

Popular posts from this blog

The MailboxConcurrency limit and using Batching in the Microsoft Graph API

If your getting an error such as Application is over its MailboxConcurrency limit while using the Microsoft Graph API this post may help you understand why. Background   The Mailbox  concurrency limit when your using the Graph API is 4 as per https://docs.microsoft.com/en-us/graph/throttling#outlook-service-limits . This is evaluated for each app ID and mailbox combination so this means you can have different apps running under the same credentials and the poor behavior of one won't cause the other to be throttled. If you compared that to EWS you could have up to 27 concurrent connections but they are shared across all apps on a first come first served basis. Batching Batching in the Graph API is a way of combining multiple requests into a single HTTP request. Batching in the Exchange Mail API's EWS and MAPI has been around for a long time and its common, for email Apps to process large numbers of smaller items for a variety of reasons.  Batching in the Gr...

Sending a Message in Exchange Online via REST from an Arduino MKR1000

This is part 2 of my MKR1000 article, in this previous post  I looked at sending a Message via EWS using Basic Authentication.  In this Post I'll look at using the new Outlook REST API  which requires using OAuth authentication to get an Access Token. The prerequisites for this sketch are the same as in the other post with the addition of the ArduinoJson library  https://github.com/bblanchon/ArduinoJson  which is used to parse the Authentication Results to extract the Access Token. Also the SSL certificates for the login.windows.net  and outlook.office365.com need to be uploaded to the devices using the wifi101 Firmware updater. To use Token Authentication you need to register an Application in Azure https://msdn.microsoft.com/en-us/office/office365/howto/add-common-consent-manually  with the Mail.Send permission. The application should be a Native Client app that use the Out of Band Callback urn:ietf:wg:oauth:2.0:oob. You ...

How to test SMTP using Opportunistic TLS with Powershell and grab the public certificate a SMTP server is using

Most email services these day employ Opportunistic TLS when trying to send Messages which means that wherever possible the Messages will be encrypted rather then the plain text legacy of SMTP.  This method was defined in RFC 3207 "SMTP Service Extension for Secure SMTP over Transport Layer Security" and  there's a quite a good explanation of Opportunistic TLS on Wikipedia  https://en.wikipedia.org/wiki/Opportunistic_TLS .  This is used for both Server to Server (eg MTA to MTA) and Client to server (Eg a Message client like Outlook which acts as a MSA) the later being generally Authenticated. Basically it allows you to have a normal plain text SMTP conversation that is then upgraded to TLS using the STARTTLS verb. Not all servers will support this verb so if its not supported then a message is just sent as Plain text. TLS relies on PKI certificates and the administrative issue s that come around certificate management like expired certificates which is why ...
All sample scripts and source code is provided by for illustrative purposes only. All examples are untested in different environments and therefore, I cannot guarantee or imply reliability, serviceability, or function of these programs.

All code contained herein is provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.