Search code examples
c#outlookcalendarexchange-serverexchangewebservices

Create appointments in Outlook calenders of employees using C# and Exchange 2019


I can't show any code here yet, because i'm still analyzing if it is possible to realize this.

In our company we have a virtual system to make and manage leave applications. I should now check if it would be possible to enter an approved vacation in the Outlook calendar of the applicant.

I would need a central solution which remotely accesses the calendar and enters the appointments. We currently use the on premise solution of Mircosoft Exchange 2019 and Office 365.

During my research I came across EWS but it seems that Exchange 2019 does not support it anymore. Is there possibly another solution which I could use? Basically I would like to realize a solution with C# but I would also be able to realize a Powershell or Java solution. But most of the time I did not find a real solution.

Most of the time the examples are always local on the machines or using an older Exchange Server like 2013. I haven't found reliable information for 2019 yet. I hope someone here can help me or give me a hint. Or it would also be helpful to say if it is not possible.

Best regards!


I am currently working on a solution. I will post the code when I am successfull!**


Solution

  • I did choose a little bit different approach but I was able to make it fully work. I now can create events and delete them if needed.

        public void UpdateCalender()
        {
            ExchangeService Service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
            Uri Url = new Uri("https://localmaildomain.sys/EWS/Exchange.asmx");
            Service.Url =  Url;
            Service.Credentials = new NetworkCredential("service_user","service_password");
    
            Folder inboxFolder = Folder.Bind(Service, new FolderId(WellKnownFolderName.Calendar, Temp.UserMail));
    
            foreach (var entry in Temp.Positions)
            {
                if (!entry.Storno)
                {
                    try
                    {
                        Appointment appointment = new Appointment(Service);
                        appointment.Subject = $"Urlaub/Vacation ({entry.Type})";
                        appointment.Body = $"{entry.Comment}";
                        appointment.IsAllDayEvent = true;
                        appointment.Start = entry.Date.AddSeconds(1);
                        appointment.End = entry.Date.AddSeconds(1);
                        appointment.LegacyFreeBusyStatus = LegacyFreeBusyStatus.OOF;
                        appointment.Save(inboxFolder.Id, SendInvitationsMode.SendToNone);
                    }
                    catch (Exception Ex)
                    {
                        Console.WriteLine($"Calender item could not be created! Exception: {Ex.ToString()}");
                    }
                }
                else
                {
                    CalendarView view = new CalendarView(entry.Date, entry.Date);
                    FindItemsResults<Appointment> results = Service.FindAppointments(inboxFolder.Id,view);
    
                    foreach (var appointment in results)
                    {
                        if (appointment.Subject == $"Urlaub/Vacation ({entry.Type})" && appointment.Start == entry.Date)
                        {
                            try
                            {
                                appointment.Delete(DeleteMode.MoveToDeletedItems);
                            }
                            catch(Exception Ex)
                            {
                                Console.WriteLine($"Calender item could not be deleted! Exception: {Ex.ToString()}");
                            }
                            break;
                        }
                    }
                }
            }
        }