Link to home
Start Free TrialLog in
Avatar of Black_Flag
Black_FlagFlag for United States of America

asked on

Creating Outlook Event - Client Side

I am attempting to create an outlook event client side after the user signs up in my volunteer application I am creating.  After they select their available start/end time to work the event I want to send the event to their calendar in outlook 2003.

When I run this locally on my computer from my code behind in .Net, it works:
Dim oApp As Outlook.Application = New Outlook.Application
Dim missing As Object = System.Reflection.Missing.Value
Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")
oNS.Logon()
Dim oAppt As Outlook.AppointmentItem = oApp.CreateItem(Outlook.OlItemType.olAppointmentItem)
 oAppt.MeetingStatus = Outlook.OlMeetingStatus.olMeeting
oAppt.Subject = "Subject"
oAppt.Body = "BodY"
oAppt.Location = "Location"
oAppt.Start = Convert.ToDateTime("6/17/2008")
oAppt.End = Convert.ToDateTime("6/17/2008")
oAppt.ReminderSet = True
oAppt.ReminderMinutesBeforeStart = 15
oAppt.BusyStatus = Outlook.OlBusyStatus.olBusy  '  olBusy
oAppt.IsOnlineMeeting = False
oAppt.Save()
oNS.Logoff()
oNS = Nothing
oAppt = Nothing
oApp = Nothing

* But when I run the code on our production server, the calendare event is never created, no errors, nothing....

So I believe I need to do this with a client side script in my code behind:
            Response.Write("<SCRIPT LANGUAGE=""text/vbscript"">" & vbCrLf)
            Response.Write("Const olAppointmentItem = 1;" & vbCrLf)
            Response.Write("objOutlook = CreateObject('Outlook.Application');" & vbCrLf)
            Response.Write("objAppointment = objOutlook.CreateItem(olAppointmentItem);" & vbCrLf)
            Response.Write("objAppointment.Start = #6/17/2008 11:00:00 AM#;" & vbCrLf)
            Response.Write("objAppointment.Duration = 60;" & vbCrLf)
            Response.Write("objAppointment.Subject = 'SUBJECT';" & vbCrLf)
            Response.Write("objAppointment.Body = 'BODY';" & vbCrLf)
            Response.Write("objAppointment.Location = 'LOCATION';" & vbCrLf)
            Response.Write("objAppointment.ReminderMinutesBeforeStart = 15;" & vbCrLf)
            Response.Write("objAppointment.ReminderSet = True;" & vbCrLf)
            Response.Write("objAppointment.Save();" & vbCrLf)
            Response.Write("</SCRIPT>")

I tried the above but it did not work locally or on the production server...

Any ideas? Thanks in advance..

Sam
Avatar of Black_Flag
Black_Flag
Flag of United States of America image

ASKER

CORRECTION:
*This works locally but NOT on the production server:

            Response.Write("<SCRIPT LANGUAGE=""text/vbscript"">" & vbCrLf)
            Response.Write("Const olAppointmentItem = 1;" & vbCrLf)
            Response.Write("objOutlook = CreateObject('Outlook.Application');" & vbCrLf)
            Response.Write("objAppointment = objOutlook.CreateItem(olAppointmentItem);" & vbCrLf)
            Response.Write("objAppointment.Start = #6/17/2008 11:00:00 AM#;" & vbCrLf)
            Response.Write("objAppointment.Duration = 60;" & vbCrLf)
            Response.Write("objAppointment.Subject = 'SUBJECT';" & vbCrLf)
            Response.Write("objAppointment.Body = 'BODY';" & vbCrLf)
            Response.Write("objAppointment.Location = 'LOCATION';" & vbCrLf)
            Response.Write("objAppointment.ReminderMinutesBeforeStart = 15;" & vbCrLf)
            Response.Write("objAppointment.ReminderSet = True;" & vbCrLf)
            Response.Write("objAppointment.Save();" & vbCrLf)
            Response.Write("</SCRIPT>")
Avatar of molku
molku

Your code will try to interact with Outlook on the web server, not the client. It may be possible with client side vb script, but I'm doubtful and it would only work in IE.
You may be able to email an appointment to the user:
https://www.experts-exchange.com/questions/21551707/Sending-an-Outlook-VCalendar-Appointment-in-a-CDO-Message-Email.html
Would this work?

http://msdn.microsoft.com/en-us/library/bb655909.aspx

I added the code, changed my time zones to use the year 2008, and I am getting an error, saying that there is an error in the .ics file that was sent.....
HERE IS WHAT I AM USING FROM THE COMMENT ABOVE:

            Dim sbICSFile As StringBuilder = New StringBuilder()
            Dim dtNow As DateTime = DateTime.Now

            sbICSFile.AppendLine("BEGIN:VCALENDAR")
            sbICSFile.AppendLine("VERSION:2.0")
            sbICSFile.AppendLine("PRODID:-//AkonaDev/CalendarAppointment")
            sbICSFile.AppendLine("CALSCALE:GREGORIAN")
            sbICSFile.AppendLine("BEGIN:VEVENT")
            ' Define time zones.
            ' US/Eastern
            sbICSFile.AppendLine("BEGIN:VTIMEZONE")
            sbICSFile.AppendLine("TZID:US/Eastern")
            sbICSFile.AppendLine("BEGIN:STANDARD")
            sbICSFile.AppendLine("DTSTART:20081104T020000")
            sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11")
            sbICSFile.AppendLine("TZOFFSETFROM:-0400")
            sbICSFile.AppendLine("TZOFFSETTO:-0500")
            sbICSFile.AppendLine("TZNAME:EST")
            sbICSFile.AppendLine("END:STANDARD")
            sbICSFile.AppendLine("BEGIN:DAYLIGHT")
            sbICSFile.AppendLine("DTSTART:20080311T020000")
            sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3")
            sbICSFile.AppendLine("TZOFFSETFROM:-0500")
            sbICSFile.AppendLine("TZOFFSETTO:-0400")
            sbICSFile.AppendLine("TZNAME:EDT")
            sbICSFile.AppendLine("END:DAYLIGHT")
            sbICSFile.AppendLine("END:VTIMEZONE")

            ' US/Central
            sbICSFile.AppendLine("BEGIN:VTIMEZONE")
            sbICSFile.AppendLine("TZID:US/Central")
            sbICSFile.AppendLine("BEGIN:STANDARD")
            sbICSFile.AppendLine("DTSTART:20081104T020000")
            sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11")
            sbICSFile.AppendLine("TZOFFSETFROM:-0500")
            sbICSFile.AppendLine("TZOFFSETTO:-0600")
            sbICSFile.AppendLine("TZNAME:CST")
            sbICSFile.AppendLine("END:STANDARD")
            sbICSFile.AppendLine("BEGIN:DAYLIGHT")
            sbICSFile.AppendLine("DTSTART:20080311T020000")
            sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3")
            sbICSFile.AppendLine("TZOFFSETFROM:-0600")
            sbICSFile.AppendLine("TZOFFSETTO:-0500")
            sbICSFile.AppendLine("TZNAME:CDT")
            sbICSFile.AppendLine("END:DAYLIGHT")

            sbICSFile.AppendLine("END:VTIMEZONE")

            ' US/Mountain
            sbICSFile.AppendLine("BEGIN:VTIMEZONE")
            sbICSFile.AppendLine("TZID:US/Mountain")
            sbICSFile.AppendLine("BEGIN:STANDARD")
            sbICSFile.AppendLine("DTSTART:20081104T020000")
            sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11")
            sbICSFile.AppendLine("TZOFFSETFROM:-0600")
            sbICSFile.AppendLine("TZOFFSETTO:-0700")
            sbICSFile.AppendLine("TZNAME:MST")
            sbICSFile.AppendLine("END:STANDARD")
            sbICSFile.AppendLine("BEGIN:DAYLIGHT")
            sbICSFile.AppendLine("DTSTART:20080311T020000")
            sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3")
            sbICSFile.AppendLine("TZOFFSETFROM:-0700")
            sbICSFile.AppendLine("TZOFFSETTO:-0600")
            sbICSFile.AppendLine("TZNAME:MDT")
            sbICSFile.AppendLine("END:DAYLIGHT")
            sbICSFile.AppendLine("END:VTIMEZONE")

            ' US/Pacific
            sbICSFile.AppendLine("BEGIN:VTIMEZONE")
            sbICSFile.AppendLine("TZID:US/Pacific")
            sbICSFile.AppendLine("BEGIN:STANDARD")
            sbICSFile.AppendLine("DTSTART:20081104T020000")
            sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11")
            sbICSFile.AppendLine("TZOFFSETFROM:-0700")
            sbICSFile.AppendLine("TZOFFSETTO:-0800")
            sbICSFile.AppendLine("TZNAME:PST")
            sbICSFile.AppendLine("END:STANDARD")
            sbICSFile.AppendLine("BEGIN:DAYLIGHT")
            sbICSFile.AppendLine("DTSTART:20080311T020000")
            sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3")
            sbICSFile.AppendLine("TZOFFSETFROM:-0800")
            sbICSFile.AppendLine("TZOFFSETTO:-0700")
            sbICSFile.AppendLine("TZNAME:PDT")
            sbICSFile.AppendLine("END:DAYLIGHT")
            sbICSFile.AppendLine("END:VTIMEZONE")

            ' Define the event
            sbICSFile.Append("DTSTART;TZID=" + "US/Central" + ":")
            sbICSFile.Append(Now().Year.ToString())
            sbICSFile.Append(FormatDateTimeValue(Now().Month))
            sbICSFile.Append(FormatDateTimeValue(Now().Day) + "T")
            sbICSFile.AppendLine("10:00 AM")

            sbICSFile.Append("DTEND;TZID=" + "US/Central" + ":")
            sbICSFile.Append(Now().Year.ToString())
            sbICSFile.Append(FormatDateTimeValue(Now().Month))
            sbICSFile.Append(FormatDateTimeValue(Now().Day) + "T")
            sbICSFile.AppendLine("11:00 AM")

            sbICSFile.AppendLine("SUMMARY:SUMMARY")
            sbICSFile.AppendLine("DESCRIPTION:EVENT")
            sbICSFile.AppendLine("UID:1")
            sbICSFile.AppendLine("SEQUENCE:0")

            sbICSFile.Append("DTSTAMP:" + dtNow.Year.ToString())
            sbICSFile.Append(FormatDateTimeValue(dtNow.Month))
            sbICSFile.Append(FormatDateTimeValue(dtNow.Day) + "T")
            sbICSFile.Append(FormatDateTimeValue(dtNow.Hour))
            sbICSFile.AppendLine(FormatDateTimeValue(dtNow.Minute) + "00")

            sbICSFile.AppendLine("END:VEVENT")
            sbICSFile.AppendLine("END:VCALENDAR")

            Response.ContentType = "text/calendar"
            Response.AddHeader("content-disposition", _
                "attachment; filename=CalendarEvent1.ics")
            Response.Write(sbICSFile)
            Response.End()


*Errors out in outlook - Cannot import vCalendar file.  One or more parameter values are not valid.
I had no idea you could do this. The code in the link you posted screams for a more userfriendly component (probably is one already....).
Giving it a quick once over, I see two things that could be the problem.
First (And I doubt this is your issue, but...) the article refers to Office 2007 and you say you are using 2003.
Second, the code is writing the StringBuilder to the response. I think this is an error it should be writing the string value of the StringBuilder.
Response.Write(sbICSFile.ToString())

Open in new window

Ok, I believe you are right about 2007 vs. 2003 as I am using 2003 here.

I changed to Response.Write(sbICSFile.ToString())....still got the error.

Ok, let me go back to trying to email the event.  When I first tried this, I got the same error.  So, let me try this and post back.
Good 'ol Code Project. You may want to look at this to help build your vcalendar file.
http://www.codeproject.com/KB/vb/vcalendar.aspx
ASKER CERTIFIED SOLUTION
Avatar of molku
molku

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
           Dim fileName As String = Server.MapPath("Event.vcs")
            Dim writer As System.IO.StreamWriter = New System.IO.StreamWriter(fileName)
            writer.WriteLine("BEGIN:VCALENDAR")
            writer.WriteLine("PRODID:-//Microsoft Corporation//Outlook MIMEDIR//EN")
            writer.WriteLine("VERSION:1.0")
            writer.WriteLine("BEGIN:VEVENT")
            Dim dueDate As DateTime
            dueDate = Now()
            writer.WriteLine("DTSTART:" + dueDate.Year.ToString() + dueDate.Month.ToString() + dueDate.Day.ToString() + "T170000Z")
            writer.WriteLine("DTEND:" + dueDate.Year.ToString() + dueDate.Month.ToString() + dueDate.Day.ToString() + "T170000Z")
            'Lets set the appointment location to the actual location of the asset.
            writer.WriteLine("LOCATION:TEST")
            writer.WriteLine("CATEGORIES:Maintenance")
            'Toss the description of maintenance into the notes field.
            writer.WriteLine("DESCRIPTION;ENCODING=QUOTED-PRINTABLE:" + "Notes" + "=0D=0A")
            'For summary, let's tell them what kind of asset it is
            writer.WriteLine("SUMMARY:" + "TEST" + " Maintenance")
            writer.WriteLine("PRIORITY:3")
            writer.WriteLine("END:VEVENT")
            writer.WriteLine("END:VCALENDAR")
            writer.Close()


Here is what it looks like in notepad:
BEGIN:VCALENDAR
PRODID:-//Microsoft Corporation//Outlook MIMEDIR//EN
VERSION:1.0
BEGIN:VEVENT
DTSTART:2008617T170000Z
DTEND:2008617T170000Z
LOCATION:TEST
CATEGORIES:Maintenance
DESCRIPTION;ENCODING=QUOTED-PRINTABLE:Notes=0D=0A
SUMMARY:TEST Maintenance
PRIORITY:3
END:VEVENT
END:VCALENDAR
I saved a test appointment like you suggested from oulook and then opened it up in notepad and compared and found some items that I was leaving out.  It works now!

Thanks for the help!