How To Create Outlook Appointments from a LightSwitch Application
Last post I showed how to create email in a couple different ways, one of them being from the LightSwitch UI on the client side that used COM to automate Outlook to create an email. If you missed it here it is:
How To Send HTML Email from a LightSwitch Application
This generated some questions about how to automate Outlook to do other things like add appointments to someone’s calendar. So because of this interest, I’ve decided to continue my series on Office automation from LightSwitch :-). This time I’ll show how to create a client-side helper class that creates and sends appointments through Outlook.
Creating the Outlook Helper Class
We need a helper class on the client so we can call if from a button on our screen. You do this by selecting the “File View” on the Solution Explorer, right-click on the Client project and select Add –> New Class. I named the class “OutlookAppointmentHelper” for this example.
This helper class uses COM automation, a feature of Silverlight 4 and higher. So first we need to check if we’re running out-of-browser on a Windows machine by checking the AutomationFactory.IsAvailable property. Next we need to get a reference to Outlook, opening the application if it’s not already open. The rest of the code just creates the appointment and sends it. In the code below, you could also comment out the call to Send and instead call Display to allow the user to modify the appointment first. NOTE: The key piece of code that enables the sending of the appointment to the toAddress is to set the MeetingStatus property to 1 (olMeeting), otherwise the appointment will just stay on the user’s calendar and wont be sent to the recipient.
Imports System.Runtime.InteropServices.Automation
Public Class OutlookAppointmentHelper
Const olAppointmentItem As Integer = 1
Const olMeeting As Integer = 1
Shared Function CreateOutlookAppointment(ByVal toAddress As String,
ByVal subject As String,
ByVal body As String,
ByVal location As String,
ByVal startDateTime As Date,
ByVal endDateTime As Date) As Boolean
Dim result = False
Try
Dim outlook As Object = Nothing
If AutomationFactory.IsAvailable Then
Try
'Get the reference to the open Outlook App
outlook = AutomationFactory.GetObject("Outlook.Application")
Catch ex As Exception 'If Outlook isn't open, then an error will be thrown.
' Try to open the application
outlook = AutomationFactory.CreateObject("Outlook.Application")
End Try
If outlook IsNot Nothing Then
'Create the Appointment
' Outlook object model (OM) reference:
' https://msdn.microsoft.com/en-us/library/ff870566.aspx
' Appointment Item members:
' https://msdn.microsoft.com/en-us/library/ff869026.aspx
Dim appt = outlook.CreateItem(olAppointmentItem)
With appt
.Body = body
.Subject = subject
.Start = startDateTime
.End = endDateTime
.Location = location
.MeetingStatus = olMeeting
.Recipients.Add(toAddress)
.Save()
'.Display()
.Send()
result = True
End With
End If
End If
Catch ex As Exception
Throw New InvalidOperationException("Failed to create Appointment.", ex)
End Try
Return result
End Function
End Class
Also note that if you want to add multiple recipients to the appointment you could pass in an array or List(Of String) and loop through that calling .Recipients.Add for each one:
Shared Function CreateOutlookAppointment(ByVal toAddress As List(Of String), ...rest of code
.
.
.
Dim appt = outlook.CreateItem(olAppointmentItem)
With appt
.Body = body
.Subject = subject
.Start = startDateTime
.End = endDateTime
.Location = location
.MeetingStatus = olMeeting
For Each addr In toAddress<br> .Recipients.Add(addr)
Next
.Save()
'.Display()
.Send()
End With
You can also add required and optional attendees by setting appropriate properties. Take a look at the object model for the Appointment item for more details.
Calling Code from a Button on a Screen
Here’s how you add a command button to a screen. In the Execute method for the command button is where we add the code to create the appointment. I also want to have the button disabled if AutomationFactory.IsAvailable is False and you check that in the CanExecute method. Here I’m working with an Appointment entity in my data model but the data to feed the appointment can come from anywhere. So here’s the code in my screen:
Private Sub CreateOutlookAppt_CanExecute(ByRef result As Boolean)
result = System.Runtime.InteropServices.Automation.AutomationFactory.IsAvailable
End Sub
Private Sub CreateOutlookAppt_Execute()
' Schedule the appointment via Outlook
With Me.Appointment
If .Customer.Email <> "" Then
If OutlookAppointmentHelper.CreateOutlookAppointment(.Customer.Email,
.Subject,
.Notes,
.Location,
.StartTime,
.EndTime) Then
Me.ShowMessageBox("Appointment has been sent.")
End If
Else
Me.ShowMessageBox("This customer does not have an email address",
"Missing Email Address",
MessageBoxOption.Ok)
End If
End With
End Sub
Run this and you will see that when the user clicks the button the appointment is sent and it appears on the user’s calendar.
Enjoy!