I'm trying to create a program that allows me to add multiple appointments/events to multiple users (without using a shared calendar).
The problem arrives when I try to impersonate someone (I'm admin so I think that I don’t have "rights" problems), I get an error System.NullReferenceException : 'Object reference not set to an instance of an object.' when the appoinment is saved (not when the impersonation is created, I don't understand why)
I don’t know what object I have to instantiate
If I don’t impersonate, the code works. The appointment is created
try
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
service.UseDefaultCredentials = true;
service.Credentials = new WebCredentials(userEmail, userPassword);
service.Url = new Uri("https://outlook.office365.com/ews/exchange.asmx");
service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, destEmail);
//service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, myEmail);
Appointment appointment = new Appointment(service);
// Set the properties on the appointment object to create the appointment.
appointment.Subject = subject;
appointment.Body = "Test Event";
appointment.Start = DateTime.Now.AddDays(2);
appointment.End = appointment.Start.AddHours(1);
appointment.Location = "Here";
appointment.ReminderDueBy = DateTime.Now;
// Save the appointment to your calendar.
appointment.Save();
// Verify that the appointment was created by using the appointment's item ID.
Item item = Item.Bind(service, appointment.Id, new PropertySet(ItemSchema.Subject));
Console.WriteLine("Appoinement created");
}
catch (Exception ex)
{
Console.WriteLine("The following error occurred: " + ex.Message);
}
Error when I try to save the appoinement
-------------------- Update 02.07.2021 Thanks for the remarks.
I removed service.UseDefaultCredentials = true
, that's dummy if a set new credentials in the next line.
The Appointment class is the default Microsoft.Exchange.WebServices.Data.Appointment, i didnt change anything there.
IT WAS a security/rights problem. Eventhough I'm admin, admins doesn't have Impersonnation ritghs by default. The thing is that I added to myself the impersonation rights needed, and it worked. Thanks
It worked for a while. It stopped working without reason, i didnt change anything.
So I waited a few hours, tryed again, still didnt work and then I re-added the rights, and it works again.
Thanks you very much for the comments/hints