Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an appointment in someone else's outlook calendar with C#

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.

like image 257
Quinten Henry Avatar asked Feb 23 '16 10:02

Quinten Henry


People also ask

Why can't I add an appointment to a shared Outlook calendar?

This issue occurs because the user does not have the required permissions to create a meeting in the calendar of the shared mailbox.

How do I add an event to a shared calendar in Outlook?

In the left pane, under Groups, make sure your group is selected. Select a time on the calendar when you want to schedule the event. At the top of the event compose window, select Calendar and choose the group calendar from the drop-down menu. Enter a title, location, and end time in the details window.

Can I cc someone on a calendar invite?

To CC (or BCC) someone to a meeting To CC someone to a meeting, you can use one of three methods to place the address in the Optional attendee field. (To BCC, use the Resources field and see the Notes, below.) Click in the Attendance column, and change the attendance for the person to Optional Attendee.

Can I color code someone else's Outlook calendar?

If the user opening a calendar shared with them doesn't have the correct permissions, they can add the Category name to their own Category list to display it with a color. Click the New button. Outlook will assign the next unused color; change the color and set a shortcut key, if desired, then click Save.


1 Answers

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("[email protected]", "password");
            service.Url = new Uri("https://mail.domain.com/EWS/Exchange.asmx");
            service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "[email protected]");




            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);
        }
like image 53
Quinten Henry Avatar answered Oct 14 '22 16:10

Quinten Henry