In this post I'm going to continue looking at the special operations in EWS that allows you to get and set the unconventional data that is stored in an Exchange mailbox like the FreeBusy information and the Out of Office Message. First off lets look at the FreeBusyTime.
Getting Freebusy time
Freebusy information is one of the more useful calendar features that Exchange offers in EWS the ability to access the freebusy information is provided by the GetUserAvailiblity operation. This operation can be useful in a number of scenarios especially when you may want to query a large number of calendars. One example is a freebusy board or meeting room website
When you make a FreeBusy request there are two constraints on the request that are outlined in http://technet.microsoft.com/en-us/library/hh310374.aspx "By default, Exchange 2007 accepts availability requests for 42 days of free/busy information and Exchange 2010 may request 62 days of free/busy information. If the request exceeds the default 42 limit imposed by Exchange 2007, the request will fail." The other constraint in the number of users you can query at one time is limited to 100 per call.
The detail level of information that is returned by this operation is also controlled by the value of RequestedFreeBusyView that you set. Essentially two different data-sets could be returned by this operation the first is the MergedFreeBusyStatus which are the FreeBusy Slots from a mailbox by default in 30 minutes increments. The second dataset is a detailed array of calendar appointment during the freebusy period if you have requested Calendar Details and you have enough rights on the Calendar to see Subject and Location information (Otherwise you will just get Start and EndTimes).
Lets look at an example of getting the freebusy time with the EWS Managed API for one user is this example we look at the CalendarDetails array to show the information that can be returned.
One useful example is to do this for a Group of users so first you will need a Distribution Group and we will then use the ExandGroup EWS operation to get all the members of that group which will give us all the Email address's of the users that we will then get the FreeBusy information for. You also need some code to ensure you can relate the information in the CalendarDetails array to that of the request because by default in this array there is no information relating the recipient this data belongs to. (Not you can actually use the Distribution Groups address instead of doing the expand but if you do this the results do get merged into one Entry which is hard to separate).
Out of Office Information
EWS provides access to the Out of Office information via GetUserOofSettings and SetUserOofSettings Operations. Or one other way of displaying the OOF setting for a user which doesn't require rights on a users mailbox is to use MailTips. Using the OOF via GetUserOofSettings/SetUserOofSettings setting is pretty easy lets looks at the basics of Getting and Displaying the OOF settings.
To set the OOF State and or the Message property you should first get
the current setting modify the property you want to modify and then
update the OOF.
eg to set the OOF of the currently logged on user you could use the following code.
An example of using MailTips to get the OOF needs a little more code because there are no methods in the EWS Managed API to make use of the MailTips operations you need to use some Raw XML instead.
Getting Freebusy time
Freebusy information is one of the more useful calendar features that Exchange offers in EWS the ability to access the freebusy information is provided by the GetUserAvailiblity operation. This operation can be useful in a number of scenarios especially when you may want to query a large number of calendars. One example is a freebusy board or meeting room website
When you make a FreeBusy request there are two constraints on the request that are outlined in http://technet.microsoft.com/en-us/library/hh310374.aspx "By default, Exchange 2007 accepts availability requests for 42 days of free/busy information and Exchange 2010 may request 62 days of free/busy information. If the request exceeds the default 42 limit imposed by Exchange 2007, the request will fail." The other constraint in the number of users you can query at one time is limited to 100 per call.
The detail level of information that is returned by this operation is also controlled by the value of RequestedFreeBusyView that you set. Essentially two different data-sets could be returned by this operation the first is the MergedFreeBusyStatus which are the FreeBusy Slots from a mailbox by default in 30 minutes increments. The second dataset is a detailed array of calendar appointment during the freebusy period if you have requested Calendar Details and you have enough rights on the Calendar to see Subject and Location information (Otherwise you will just get Start and EndTimes).
Lets look at an example of getting the freebusy time with the EWS Managed API for one user is this example we look at the CalendarDetails array to show the information that can be returned.
- $StartTime = [DateTime]::Parse([DateTime]::Now.ToString("yyyy-MM-dd 0:00"))
- $EndTime = $StartTime.AddDays(7)
- $drDuration = new-object Microsoft.Exchange.WebServices.Data.TimeWindow($StartTime,$EndTime)
- $AvailabilityOptions = new-object Microsoft.Exchange.WebServices.Data.AvailabilityOptions
- $AvailabilityOptions.RequestedFreeBusyView = [Microsoft.Exchange.WebServices.Data.FreeBusyViewType]::DetailedMerged
- $listtype = ("System.Collections.Generic.List"+'`'+"1") -as "Type"
- $listtype = $listtype.MakeGenericType("Microsoft.Exchange.WebServices.Data.AttendeeInfo" -as "Type")
- $Attendeesbatch = [Activator]::CreateInstance($listtype)
- $Attendee = new-object Microsoft.Exchange.WebServices.Data.AttendeeInfo("user@domain.com")
- $Attendeesbatch.add($Attendee)
- $availresponse = $service.GetUserAvailability($Attendeesbatch,$drDuration,[Microsoft.Exchange.WebServices.Data.AvailabilityData]::FreeBusy,$AvailabilityOptions)
- foreach($avail in $availresponse.AttendeesAvailability){
- foreach($cvtEnt in $avail.CalendarEvents){
- "Start : " + $cvtEnt.StartTime
- "End : " + $cvtEnt.EndTime
- "Subject : " + $cvtEnt.Details.Subject
- "Location : " + $cvtEnt.Details.Location
- ""
- }
- }
- $GroupAddress = "testinggroup@domain.com"
- $StartTime = [DateTime]::Parse([DateTime]::Now.ToString("yyyy-MM-dd 0:00"))
- $EndTime = $StartTime.AddDays(7)
- $drDuration = new-object Microsoft.Exchange.WebServices.Data.TimeWindow($StartTime,$EndTime)
- $AvailabilityOptions = new-object Microsoft.Exchange.WebServices.Data.AvailabilityOptions
- $AvailabilityOptions.RequestedFreeBusyView = [Microsoft.Exchange.WebServices.Data.FreeBusyViewType]::DetailedMerged
- $listtype = ("System.Collections.Generic.List"+'`'+"1") -as "Type"
- $listtype = $listtype.MakeGenericType("Microsoft.Exchange.WebServices.Data.AttendeeInfo" -as "Type")
- $Attendeesbatch = [Activator]::CreateInstance($listtype)
- $GroupMembers = $service.ExpandGroup($GroupAddress)
- $adrHash = @{}
- $adcnt = 0
- foreach($Address in $GroupMembers.Members){
- $Attendee = new-object Microsoft.Exchange.WebServices.Data.AttendeeInfo($Address.Address)
- $Attendeesbatch.add($Attendee)
- $adrHash.add($adcnt,$Address.Address)
- $adcnt++
- $Address.Address
- }
- $adcnt = 0
- $availresponse = $service.GetUserAvailability($Attendeesbatch,$drDuration,[Microsoft.Exchange.WebServices.Data.AvailabilityData]::FreeBusy,$AvailabilityOptions)
- foreach($avail in $availresponse.AttendeesAvailability){
- foreach($cvtEnt in $avail.CalendarEvents){
- "Mailbox : " + $adrHash[$adcnt]
- "Start : " + $cvtEnt.StartTime
- "End : " + $cvtEnt.EndTime
- "Subject : " + $cvtEnt.Details.Subject
- "Location : " + $cvtEnt.Details.Location
- ""
- }
- $adcnt++
- }
EWS provides access to the Out of Office information via GetUserOofSettings and SetUserOofSettings Operations. Or one other way of displaying the OOF setting for a user which doesn't require rights on a users mailbox is to use MailTips. Using the OOF via GetUserOofSettings/SetUserOofSettings setting is pretty easy lets looks at the basics of Getting and Displaying the OOF settings.
- $oofSetting = $service.GetUserOofSettings("user@domain.com")
- "OOF State : " + $oofSetting.State
- "InternalReply : " + $oofSetting.InternalReply
- "ExternalReply : " + $oofSetting.ExternalReply
- "AllowExternalOof : " + $oofSetting.AllowExternalOof
eg to set the OOF of the currently logged on user you could use the following code.
- $MailboxName = "user@domain.com"
- $oofSetting = $service.GetUserOofSettings($MailboxName)
- $oofSetting.State.ToString()
- $oofSetting.InternalReply = new-object Microsoft.Exchange.WebServices.Data.OofReply("Test 123")
- $oofSetting.ExternalReply.ToString()
- $oofSetting.State = [Microsoft.Exchange.WebServices.Data.OofState]::Enabled
- $service.SetUserOofSettings($MailboxName, $oofSetting);
An example of using MailTips to get the OOF needs a little more code because there are no methods in the EWS Managed API to make use of the MailTips operations you need to use some Raw XML instead.
- $MailboxName = "user@domain.com"
- $expRequest = @"
- <?xml version="1.0" encoding="utf-8"?>
- <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <soap:Header><RequestServerVersion Version="Exchange2010_SP1" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" />
- </soap:Header>
- <soap:Body>
- <GetMailTips xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">
- <SendingAs>
- <EmailAddress xmlns="http://schemas.microsoft.com/exchange/services/2006/types">$MailboxName</EmailAddress>
- </SendingAs>
- <Recipients>
- "@
- foreach($mbMailbox in $Mailboxes){
- $expRequest = $expRequest + "<Mailbox xmlns=`"http://schemas.microsoft.com/exchange/services/2006/types`"><EmailAddress>$mbMailbox</EmailAddress></Mailbox>"
- }
- $expRequest = $expRequest + "</Recipients><MailTipsRequested>OutOfOfficeMessage</MailTipsRequested></GetMailTips></soap:Body></soap:Envelope>"
- $mbMailboxFolderURI = New-Object System.Uri($service.url)
- $wrWebRequest = [System.Net.WebRequest]::Create($mbMailboxFolderURI)
- $wrWebRequest.KeepAlive = $false;
- $wrWebRequest.Headers.Set("Pragma", "no-cache");
- $wrWebRequest.Headers.Set("Translate", "f");
- $wrWebRequest.Headers.Set("Depth", "0");
- $wrWebRequest.ContentType = "text/xml";
- $wrWebRequest.ContentLength = $expRequest.Length;
- $wrWebRequest.Timeout = 60000;
- $wrWebRequest.Method = "POST";
- $wrWebRequest.Credentials = $cred
- $bqByteQuery = [System.Text.Encoding]::ASCII.GetBytes($expRequest);
- $wrWebRequest.ContentLength = $bqByteQuery.Length;
- $rsRequestStream = $wrWebRequest.GetRequestStream();
- $rsRequestStream.Write($bqByteQuery, 0, $bqByteQuery.Length);
- $rsRequestStream.Close();
- $wrWebResponse = $wrWebRequest.GetResponse();
- $rsResponseStream = $wrWebResponse.GetResponseStream()
- $sr = new-object System.IO.StreamReader($rsResponseStream);
- $rdResponseDocument = New-Object System.Xml.XmlDocument
- $rdResponseDocument.LoadXml($sr.ReadToEnd());
- $RecipientNodes = @($rdResponseDocument.getElementsByTagName("t:RecipientAddress"))
- $Datanodes = @($rdResponseDocument.getElementsByTagName("t:OutOfOffice"))
- for($ic=0;$ic -lt $RecipientNodes.length;$ic++){
- if($Datanodes[$ic].ReplyBody.Message -eq ""){
- $RecipientNodes[$ic].EmailAddress + " : In the Office"
- }
- else{
- $RecipientNodes[$ic].EmailAddress + " : Out of Office"
- }
- }