Thursday, May 28, 2015

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.

6 comments:

Anonymous said...

Hi Glen do you have the EWS code for applying retention tag to custom folders.
Phil.

microsoft erp said...

Hello! I'm happy to learn something about Microsoft Office 365 Groups, which is a new feature in the program. Thank a lot for showing how to use Office 365 Groups within the EWS Managed API.

Varun Joshi said...

Hi Glen, Do you have any code to update Mailbox Conversation Item using EWS API?
-Varun

Glen Scales said...

The updateItem operation in the only thing you would use to update an Item

Srinivasa Rao S said...

Hi Glen,
We are able to access public group's mailbox as you suggested. However we are not able to access private groups data using EWS. Can you help us how to resolve this issue?

We are getting access denied exception while accessing private groups.

Glen Scales said...

That would mean the security context you using doesn't have access to the Mailbox in question. Whatever security context you using would need to be a member of the Group and be able to access the group normal in OWA etc.