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...

EWS-FAI Module for browsing and updating Exchange Folder Associated Items from PowerShell

Folder Associated Items are hidden Items in Exchange Mailbox folders that are commonly used to hold configuration settings for various Mailbox Clients and services that use Mailboxes. Some common examples of FAI's are Categories,OWA Signatures and WorkHours there is some more detailed documentation in the https://msdn.microsoft.com/en-us/library/cc463899(v=exchg.80).aspx protocol document. In EWS these configuration items can be accessed via the UserConfiguration operation https://msdn.microsoft.com/en-us/library/office/dd899439(v=exchg.150).aspx which will give you access to either the RoamingDictionary, XMLStream or BinaryStream data properties that holds the configuration depending on what type of FAI data is being stored. I've written a number of scripts over the years that target particular FAI's (eg this one that reads the workhours  http://gsexdev.blogspot.com.au/2015/11/finding-timezone-being-used-in-mailbox.html is a good example ) but I didn't have a gene...

Sending a MimeMessage via the Microsoft Graph using the Graph SDK, MimeKit and MSAL

One of the new features added to the Microsoft Graph recently was the ability to create and send Mime Messages (you have been able to get Message as Mime for a while). This is useful in a number of different scenarios especially when trying to create a Message with inline Images which has historically been hard to do with both the Graph and EWS (if you don't use MIME). It also opens up using SMIME for encryption and a more easy migration path for sending using SMTP in some apps. MimeKit is a great open source library for parsing and creating MIME messages so it offers a really easy solution for tackling this issue. The current documentation on Send message via MIME lacks any real sample so I've put together a quick console app that use MSAL, MIME kit and the Graph SDK to send a Message via MIME. As the current Graph SDK also doesn't support sending via MIME either there is a workaround for this in the future my guess is this will be supported.
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.