7

I want to create a program which makes it possible to create an appointment in someone else's outlook calendar. For example : If someone asks their boss for five days free, their boss needs to be able to approve it and immediately make it visible in the person's outlook calendar. I already made some code in which allows you to set your own appointments. here is my code:

    public Form1()
    {
        InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)
    {
        AddAppointment("ConferenceRoom #2345", "We will discuss progression the group project.", "Group Project", new DateTime(2016, 02, 23, 15, 30, 52), new DateTime(2016, 02, 23, 20, 30, 52));
    }
    private void AddAppointment(string location, string body, string subject, DateTime startdatum, DateTime einddatum)
    {
        try
        {
            var AppOutlook = new Outlook.Application();

            Outlook.AppointmentItem newAppointment =
            (Outlook.AppointmentItem)
            AppOutlook.CreateItem(Outlook.OlItemType.olAppointmentItem);
            newAppointment.Start = startdatum;
            newAppointment.End = einddatum;
            newAppointment.Location = location;
            newAppointment.Body = body;
            newAppointment.BusyStatus=Outlook.OlBusyStatus.olTentative;
            newAppointment.AllDayEvent = true;
            newAppointment.Subject = subject;
            newAppointment.Save();
            newAppointment.Display(true);
        }
        catch (Exception ex)
        {
            MessageBox.Show("The following error occurred: " + ex.Message);
        }
    }

PS: Sorry if my english isn't great.

Alfie Goodacre
  • 2,753
  • 1
  • 13
  • 26
Quinten Henry
  • 125
  • 1
  • 2
  • 10
  • what about adding the employee as required attendee? (see https://msdn.microsoft.com/en-us/library/office/ff867639.aspx) – Breeze Feb 23 '16 at 11:48
  • an approach that takes a bit more time is the use of EWS (I've already used it, works great, but you might get problems with permissions), see for example this question on how to do it: http://stackoverflow.com/questions/2419651/using-ews-managed-api-to-create-appointments-for-other-users – Breeze Feb 23 '16 at 11:51
  • If I add someone as a required attendee, the free-days of the person would also be set in the boss his calendar, I suppose – Quinten Henry Feb 23 '16 at 12:04
  • Yes that's right. But in my company, almost every boss wanted to see in their calender when their employees had some days off – Breeze Feb 23 '16 at 12:10

2 Answers2

2

I made use of the EWS api (https://msdn.microsoft.com/en-us/library/office/dd633710(v=exchg.80).aspx)

the code i used is:

try
        {
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
            service.UseDefaultCredentials = true;
            service.Credentials = new WebCredentials("user@domain.com", "password");
            service.Url = new Uri("https://mail.domain.com/EWS/Exchange.asmx");
            service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "user2@domain.com");




            Appointment appointment = new Appointment(service);
            // Set the properties on the appointment object to create the appointment.
            appointment.Subject = "Tennis lesson";
            appointment.Body = "Focus on backhand this week.";
            appointment.Start = DateTime.Now.AddDays(2);
            appointment.End = appointment.Start.AddHours(1);
            appointment.Location = "Tennis club";
            appointment.ReminderDueBy = DateTime.Now;

            // Save the appointment to your calendar.
            appointment.Save(SendInvitationsMode.SendToNone);

            // Verify that the appointment was created by using the appointment's item ID.
            Item item = Item.Bind(service, appointment.Id, new PropertySet(ItemSchema.Subject));
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
Quinten Henry
  • 125
  • 1
  • 2
  • 10
0

There are three main ways for creating appointments in Outlook. You need to use the Items collection of the folder (Calendar folder in our case). The Add method accepts the OlItemType enumeration too and returns a newly created Outlook item. All possible ways are described in depth in the How To: Create a new Outlook Appointment item article.

     items = calendarFolder.Items;
     appItem = items.Add(Outlook.OlItemType.olAppointmentItem) as Outlook.AppointmentItem;
     appItem.Save();
     appItem.Display(true);

Note, you need to get a shared calendar folder first. The GetSharedDefaultFolder method of the Namespace class returns a Folder object that represents the specified default folder for the specified user. This method is used in a delegation scenario, where one user has delegated access to another user for one or more of their default folders (for example, their shared Calendar folder). For example:

  Sub ResolveName()  
   Dim myNamespace As Outlook.NameSpace  
   Dim myRecipient As Outlook.Recipient  
   Dim CalendarFolder As Outlook.Folder 
   Set myNamespace = Application.GetNamespace("MAPI")  
   Set myRecipient = myNamespace.CreateRecipient("Eugene Astafiev")  
   myRecipient.Resolve  
   If myRecipient.Resolved Then  
    Call ShowCalendar(myNamespace, myRecipient)  
   End If  
  End Sub 

  Sub ShowCalendar(myNamespace, myRecipient)  
   Dim CalendarFolder As Outlook.Folder 
   Set CalendarFolder = _  
   myNamespace.GetSharedDefaultFolder _  
   (myRecipient, olFolderCalendar)  
   CalendarFolder.Display  
  End Sub
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45