Customizing Outlook calendar with Advanced regions: C# sample
Today, we’ll write an add-in to display calendar specific information in Outlook 2010 using Outlook advanced regions and Add-in Express 2010.
Start of in the usual fashion by creating a new ADX COM Add-in project in Visual Studio 2010:
Complete the wizard by selecting Microsoft Office 2010 as the minimum supported Office version, Microsoft Outlook as the supported application and C# as our language of choice.
As in the previous example, add a new ADX Outlook form to you project, by selecting Add New Item… from Visual Studio’s Project menu:
This form will display the number of hours spent with each individual in the selected Outlook calendar range. I’ve added a List View control and a Microsoft Chart control to the form. You can download the Microsoft Chart Controls for .Net and samples of how to use it here. The form design looks like this:
We’ll add code to retrieve all the attendees from a range of selected days in the Outlook calendar, and display the number of hours spent with each attendee:
public void LoadData(Outlook._Items calendarItems, DateTime startDate, DateTime endDate) { Outlook.Items restrictedItems = calendarItems.Restrict( String.Format("[Start] >= {0}{1:g}{0} AND [End] <= {0}{2:g}{0}", (char)34, startDate, endDate)); try { List> timeSpent = new List>(); for (int i = 1; i < restrictedItems.Count; i++) { Outlook._AppointmentItem appItem = (Outlook._AppointmentItem)restrictedItems[i]; string[] attendees = appItem.RequiredAttendees.Split(';'); List individuals = new List(); individuals.AddRange(attendees); foreach (string individual in individuals) { Dictionary appointment = new Dictionary(); decimal duration = Convert.ToDecimal(appItem.Duration) / 60; appointment.Add(individual.Trim(), duration); timeSpent.Add(appointment); } Marshal.ReleaseComObject(appItem); } var results = timeSpent.SelectMany(d => d) .GroupBy(kvp => kvp.Key, kvp => kvp.Value) .ToDictionary(g => g.Key, g => g.Sum()); TimeSpentChart.Series["Default"].Points. DataBindXY(results, "Key", results, "Value"); TimeSpentChart.DataBind(); lvData.Items.Clear(); foreach (KeyValuePair result in results) { ListViewItem lviName = new ListViewItem(result.Key); ListViewItem.ListViewSubItem lviHours = new ListViewItem.ListViewSubItem(lviName, result.Value.ToString()); lviName.SubItems.Add(lviHours); lvData.Items.Add(lviName); } } finally { Marshal.ReleaseComObject(restrictedItems); } }
Switch back to the AddinModule designer and add a new ADXOutlookFormsManager.
Select the ADXOutlookFormsManager and click the ellipses (…) button next to its Items property. Add a new item to the forms collection by clicking the Add button. Set the following properties:
- Cached : OneInstanceForAllFolders
- ExplorerItemTypes : Appointment
- ExplorerLayout : DockBottom
- FormClassname : (The ADX Outlook form, you’ve added earlier)
I’ve set the ExplorerLayout to Dockbottom, because at the time of writing TopSubpane, BottomSubpane, RightSubpane, LeftSubpane and FolderView regions for appointment folders do not work in Outlook 2010, so DockBottom, DockTop, DockLeft or DockRight are good alternatives when working with the Outlook Calendar.
Next, add a new ADXOutlookAppEvents component to the AddinModule designer.
Add the following code to the ExplorerSelectionChange event handler:
private void adxOutlookEvents_ExplorerSelectionChange(object sender, object explorer) { Outlook._Explorer currExplorer = (Outlook._Explorer)explorer; if (currExplorer != null) { Outlook.MAPIFolder selectedFolder = currExplorer.CurrentFolder; Outlook._Items selectedItems = selectedFolder.Items; try { if (selectedItems != null && selectedItems[1] is Outlook.AppointmentItem) { Outlook._CalendarView calendarView = (Outlook.CalendarView)currExplorer.CurrentView; try { if (calendarView != null) { GraphForm explorerForm = (GraphForm) adxOlFormsCollectionItem1.GetCurrentForm( AddinExpress.OL.EmbeddedFormStates.Active); if (explorerForm != null) { explorerForm.LoadData(selectedItems, calendarView.SelectedStartTime, calendarView.SelectedEndTime); } } } finally { Marshal.ReleaseComObject(calendarView); } } } finally { Marshal.ReleaseComObject(selectedFolder); Marshal.ReleaseComObject(selectedItems); } } }
This code will refresh the data on our Outlook Form when the user selects different date ranges in the Outlook Calendar. Build and register your project and run MS Outlook.
When switching to the Calendar view, you should see the Outlook advanced region in the bottom of the Outlook window. When selecting a date range the data on the form will refresh.
And there you have it – a customized Outlook calendar with a quick visual way to display the number of hours spent with different individuals.
Thank you for reading. Until next time, keep coding!