0

I want to create appointment on outlook calendar by using EWS. I followed https://msdn.microsoft.com/en-us/library/office/dn722379(v=exchg.150).aspx link. but I am seeing this error:

"The account does not have permission to impersonate the requested user".

Please go through my code

private static ExchangeService Service
    {
        get
        {
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
            //service.Credentials = new WebCredentials("example@abc.com", password);
            service.AutodiscoverUrl("example@abc.com");
            return service;
        }
    }

private void SaveAppointment()
{
        IAppointmentFactory iappointment = new AppointmentFactory();
        List<string> lstEmail = new List<string>() {"other@abc.com"};

        CreateAppointments(Service, lstEmail, iappointment);
}

private static void CreateAppointments(ExchangeService service, List<string> emailAddresses, IAppointmentFactory factory)
{
    // Loop through the list of email addresses to add the appointment.
    foreach (var emailAddress in emailAddresses)
    {
        Console.WriteLine(string.Format("  Placing appointment in calendar for {0}.", emailAddress));

        // Set the email address of the account to get the appointment.
        service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, emailAddress);

        // Get the appointment to add.
        Microsoft.Exchange.WebServices.Data.Appointment appointment = factory.GetAppointment(service);

        // Save the appointment.
        try
        {
            if (appointment.RequiredAttendees.Count > 0)
            {
                // The appointment has attendees so send them the meeting request.
                appointment.Save(SendInvitationsMode.SendToAllAndSaveCopy);
            }
            else
            {
                // The appointment does not have attendees, so just save to calendar.
                appointment.Save(SendInvitationsMode.SendToNone);
            }
        }
        catch (ServiceResponseException ex)
        {
            Console.WriteLine(string.Format("Could not create appointment for {0}", emailAddress));
            Console.WriteLine(ex.Message);
        }
    }
}
manika
  • 187
  • 2
  • 13
  • 1
    Did you google the issue and then select the first result? http://stackoverflow.com/questions/15204411/the-account-does-not-have-permission-to-impersonate-the-requested-user – Bassie Jan 06 '17 at 17:34
  • Yes i have already gone through to this link but didn't understand that where to fix that code into my code. Can you please suggest? – manika Jan 06 '17 at 17:48

1 Answers1

1

It seems as you only have delegate access to the calendar, you want to save appointments to.

In order to get your code working, please remove the line

service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, emailAddress);

and change the Save methods like this:

appointment.Save(new FolderId(WellKnownFolderName.Calendar, new Mailbox(emailAddress)), SendInvitationsMode.SendOnlyToAllAndSaveCopy);
NotTelling
  • 462
  • 3
  • 11
  • Hi Tristan, Thanks for your reply. I have done same thing as you suggested. The above problem goes off now but a new problem is raised which is "The specified folder could not be found in the store." Do you any idea about this issue? – manika Jan 09 '17 at 13:26
  • This issue is most likely caused by an authorisation problem. Are you sure that you have been given delegate access to all Mailboxes/Calendars, which you want to save appointments in? You can find out about this by executing `DelegateInformation result = service.GetDelegates(new Mailbox("emailAddress"), true);` and printing `result` into your console or somewhere else. – NotTelling Jan 09 '17 at 13:37
  • I wrote that line of code but it is giving same exception on DelegateInformation result = service.GetDelegates(new Mailbox(emailAddress), true); line. – manika Jan 09 '17 at 15:10
  • Okay, can you please ask your sysadmin, if you have got delegate access to the specified mailboxes? He can grant them to you, if necessary. – NotTelling Jan 10 '17 at 12:12
  • Thanks and appreciated your reply :). – manika Jan 11 '17 at 13:12