Someone asked an interesting question about recurring Appointments in Exchange recently that I thought I'd expand on a bit with blog post. Recurrence in Appointments,Meeting and Tasks are one of the more useful features of Exchange, but they are also one the features that are harder for developers and anybody writing code or scripts to get a handle on. These days there is some really good technical documentation on this so I won't explain too much but firstly http://msdn.microsoft.com/EN-US/library/office/dn727655(v=exchg.150).aspx but also the Exchange Protocol Document http://msdn.microsoft.com/en-us/library/ee201188(v=exchg.80).aspx are a must to read.
To refine this down a bit with Calendar Appointments if you have a recurring Appointment you don't have separate objects for each occurrence of the recurring Appointment, the recurrence is just a pattern stored in a property on what is called the master instance of that particular Appointment, Meeting or Task. Exception information is stored as part of the Recurrence pattern (while the actual data from the Exception if there is anything different from the Master instance eg say you add an attachment to an exception this data gets stored as AttachedItem on the Master instance).
What this all means is that when it comes time to actually query the Calendar Appointments these recurrence patterns need to be expanded. To do this in EWS you use the CalendarView class and you specify a Start and End Date for your query. Exchange will then do all the hard work for you and return the expanded Appointments (and exceptions) to you.
To get back to the actual question however of how you would find expired recurring Calendar Appointments you have to forget about making an expansion query and instead you want to make a query to return a list of these master instances of recurring calendar Appointments and then filter these at the client side to work out what has expired (or if you want to do other reporting like seeing who has made non expiring appointments you can do this).
So to do this you can use the FindItem operation with a restriction on the PidLidRecurring property http://msdn.microsoft.com/en-us/library/ee157994(v=EXCHG.80).aspx which looks like this
This will then return a list of Master Appointment instances, to get the full details of the Recurrence of each appointment you need to do a GetItem on each of the master instance which in the Managed API you can use the LoadPropertiesForItems method for. This will make a batch GetItem request to the Exchange Server to return the detail on all Items your request it for.
Once you have the recurrence information you can then filter those Meetings that have an End Date (or Recurrence number) set by using the HasEnd property. Then to check the Last Occurrence you can use the LastOccurrence property. If you want to report on other things like meetings that don't have an EndDate or meetings that are expiring in the next week or month this is where you can put your own customization.
The following script will produce a report of any expired Meetings in a Mailbox's calendar and save this to a CSV file. I've put a download of this script here the code itself looks like
To refine this down a bit with Calendar Appointments if you have a recurring Appointment you don't have separate objects for each occurrence of the recurring Appointment, the recurrence is just a pattern stored in a property on what is called the master instance of that particular Appointment, Meeting or Task. Exception information is stored as part of the Recurrence pattern (while the actual data from the Exception if there is anything different from the Master instance eg say you add an attachment to an exception this data gets stored as AttachedItem on the Master instance).
What this all means is that when it comes time to actually query the Calendar Appointments these recurrence patterns need to be expanded. To do this in EWS you use the CalendarView class and you specify a Start and End Date for your query. Exchange will then do all the hard work for you and return the expanded Appointments (and exceptions) to you.
To get back to the actual question however of how you would find expired recurring Calendar Appointments you have to forget about making an expansion query and instead you want to make a query to return a list of these master instances of recurring calendar Appointments and then filter these at the client side to work out what has expired (or if you want to do other reporting like seeing who has made non expiring appointments you can do this).
So to do this you can use the FindItem operation with a restriction on the PidLidRecurring property http://msdn.microsoft.com/en-us/library/ee157994(v=EXCHG.80).aspx which looks like this
- $Recurring = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition([Microsoft.Exchange.WebServices.Data.DefaultExtendedPropertySet]::Appointment, 0x8223,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Boolean);
- $sfItemSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo($Recurring,$true)
This will then return a list of Master Appointment instances, to get the full details of the Recurrence of each appointment you need to do a GetItem on each of the master instance which in the Managed API you can use the LoadPropertiesForItems method for. This will make a batch GetItem request to the Exchange Server to return the detail on all Items your request it for.
Once you have the recurrence information you can then filter those Meetings that have an End Date (or Recurrence number) set by using the HasEnd property. Then to check the Last Occurrence you can use the LastOccurrence property. If you want to report on other things like meetings that don't have an EndDate or meetings that are expiring in the next week or month this is where you can put your own customization.
The following script will produce a report of any expired Meetings in a Mailbox's calendar and save this to a CSV file. I've put a download of this script here the code itself looks like
- ## Get the Mailbox to Access from the 1st commandline argument
- $MailboxName = $args[0]
- ## Load Managed API dll
- Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\2.1\Microsoft.Exchange.WebServices.dll"
- ## Set Exchange Version
- $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2
- ## Create Exchange Service Object
- $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)
- ## Set Credentials to use two options are availible Option1 to use explict credentials or Option 2 use the Default (logged On) credentials
- #Credentials Option 1 using UPN for the windows Account
- $psCred = Get-Credential
- $creds = New-Object System.Net.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())
- $service.Credentials = $creds
- #Credentials Option 2
- #service.UseDefaultCredentials = $true
- ## Choose to ignore any SSL Warning issues caused by Self Signed Certificates
- ## Code From http://poshcode.org/624
- ## Create a compilation environment
- $Provider=New-Object Microsoft.CSharp.CSharpCodeProvider
- $Compiler=$Provider.CreateCompiler()
- $Params=New-Object System.CodeDom.Compiler.CompilerParameters
- $Params.GenerateExecutable=$False
- $Params.GenerateInMemory=$True
- $Params.IncludeDebugInformation=$False
- $Params.ReferencedAssemblies.Add("System.DLL") | Out-Null
- $TASource=@'
- namespace Local.ToolkitExtensions.Net.CertificatePolicy{
- public class TrustAll : System.Net.ICertificatePolicy {
- public TrustAll() {
- }
- public bool CheckValidationResult(System.Net.ServicePoint sp,
- System.Security.Cryptography.X509Certificates.X509Certificate cert,
- System.Net.WebRequest req, int problem) {
- return true;
- }
- }
- }
- '@
- $TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)
- $TAAssembly=$TAResults.CompiledAssembly
- ## We now create an instance of the TrustAll and attach it to the ServicePointManager
- $TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
- [System.Net.ServicePointManager]::CertificatePolicy=$TrustAll
- ## end code from http://poshcode.org/624
- ## Set the URL of the CAS (Client Access Server) to use two options are availbe to use Autodiscover to find the CAS URL or Hardcode the CAS to use
- #CAS URL Option 1 Autodiscover
- $service.AutodiscoverUrl($MailboxName,{$true})
- "Using CAS Server : " + $Service.url
- #CAS URL Option 2 Hardcoded
- #$uri=[system.URI] "https://casservername/ews/exchange.asmx"
- #$service.Url = $uri
- ## Optional section for Exchange Impersonation
- #$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)
- # Bind to the Calendar Folder
- $folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Calendar,$MailboxName)
- $Calendar = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
- $Recurring = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition([Microsoft.Exchange.WebServices.Data.DefaultExtendedPropertySet]::Appointment, 0x8223,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Boolean);
- $sfItemSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo($Recurring,$true)
- #Define ItemView to retrive just 1000 Items
- $ivItemView = New-Object Microsoft.Exchange.WebServices.Data.ItemView(1000)
- $rptCollection = @()
- $fiItems = $null
- do{
- $psPropset= new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
- $fiItems = $service.FindItems($Calendar.Id,$sfItemSearchFilter,$ivItemView)
- if($fiItems.Items.Count -gt 0){
- [Void]$service.LoadPropertiesForItems($fiItems,$psPropset)
- foreach($Item in $fiItems.Items){
- if($Item.Recurrence.HasEnd){
- if($Item.LastOccurrence.End -lt (Get-Date)){
- $rptObj = "" | Select Mailbox,Organizer,AppointmentSubject,FirstOccuranceStart,FirstOccuranceEnd,LastOccuranceStart,LastOccuranceEnd
- Write-Host "Appointment : " $Item.Subject
- Write-Host "Last Occurance : " $Item.LastOccurrence.Start
- $rptObj.Mailbox = $MailboxName
- $rptObj.Organizer = $Item.Organizer.Name
- $rptObj.AppointmentSubject = $Item.Subject
- $rptObj.FirstOccuranceStart = $Item.FirstOccurrence.Start
- $rptObj.FirstOccuranceEnd = $Item.FirstOccurrence.End
- $rptObj.LastOccuranceStart = $Item.LastOccurrence.Start
- $rptObj.LastOccuranceEnd = $Item.LastOccurrence.End
- $rptCollection += $rptObj
- }
- }
- }
- }
- $ivItemView.Offset += $fiItems.Items.Count
- }while($fiItems.MoreAvailable -eq $true)
- $rptCollection | Export-Csv -NoTypeInformation -Path c:\Temp\$MailboxName-ExpiredRecurringMeetings.csv
- Write-Host "Report written to " c:\Temp\$MailboxName-ExpiredRecurringMeetings.csv