So I've been looking into programmatically creating calendar events for the Outlook Calendar.
I looked at the documentation here and here.
Are there any step-by-steps regarding asp.net webforms to display on .ASPX pages or exemplars of code targeted for that domain.
I tried following this example in adding an appointment to the calendar but I receive the error 'HttpApplicationState does not contain a definition for 'CreateItem' and no accessible extension method 'CreateItem' accepting a first argument of type 'HttpApplicationState' could be found (are you missing a using directive or an assembly reference?)
private void AddAppointment()
{
try
{
Outlook.AppointmentItem newAppointment = (Outlook.AppointmentItem)
Application.CreateItem(Outlook.OlItemType.olAppointmentItem);
newAppointment.Start = DateTime.Now.AddHours(2);
newAppointment.End = DateTime.Now.AddHours(3);
newAppointment.Location = "ConferenceRoom #2345";
newAppointment.Body = "We will discuss progress on the group project.";
newAppointment.AllDayEvent = false;
newAppointment.Subject = "Group Project";
newAppointment.Recipients.Add("Roger Harui");
Outlook.Recipients sentTo = newAppointment.Recipients;
Outlook.Recipient sentInvite = null;
sentInvite = sentTo.Add("Holly Holt");
sentInvite.Type = (int)Outlook.OlMeetingRecipientType.olRequired;
sentInvite = sentTo.Add("David Junca ");
sentInvite.Type = (int)Outlook.OlMeetingRecipientType.olOptional;
sentTo.ResolveAll();
newAppointment.Save();
newAppointment.Display(true);
}
catch (Exception ex)
{
Console.WriteLine("The following error occurred: " + ex.Message);
}
}
These are my using's:
using System;
using Outlook = Microsoft.Office.Interop.Outlook;
Have I just come at this completely wrong?
EDIT #1: I want to integrate the outlook calendar into the website so that users can add an appointment to their outlook calendar. I'm looking for concise documentation to achieve this in C# on an ASP.NET webforms site, and most of the documentation is for Microsoft.Office.Interop which doesn't seem to work locally for me.
The code you show in your sample is executed on the server. Usually, Outlook is not installed on the server so that you receive an error. Also, the user context on the server is often one of a service account, not of the user that initiated the request. Usually, there are different approaches that are used to create calendar items.
An easy way to enable the users of your website to add an appointment to their calendar (independent of the program they use) is to download an iCalendar-file (ICS) from your website by clicking a link on your site. This file is opened on the client and the users can save the appointment in the calendar application they use. There are various nuget packages that support you in creating the files.
You could also add a specific "Add to Outlook calendar" function to your site if it is an internal application for a company so that you know some details about the infrastructure. For instance, if the company uses Office 365, you could use Microsoft Graph to create a calendar item.
If the company uses an Exchange server, you could also use Exchange Web Services (EWS) to access an exchange server and create the appointment without using Outlook.