2

I have a requirement like I need to fetch all the users calendar events using an Admin account in Exchange service API.I tried but I am unable to achieve it so Please advise me on this. PFB code for your reference. I am using Exchange server Version 2013_SP1. In the below code I can able to get only one user calendar event details.

public List<EWSModel> GetCalendarEventDetails()
    {
        List<EWSModel> objCalItems = new List<EWSModel>();
        try
        {
            ExchangeService service = CallEWSAPIService();
            DateTime startDate = DateTime.Now.AddDays(-30);
            DateTime endDate = DateTime.Now;
            CalendarFolder calendar = CalendarFolder.Bind(service, WellKnownFolderName.Calendar, new PropertySet());
            CalendarView cView = new CalendarView(startDate, endDate);
            cView.PropertySet = new PropertySet(AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.End);
            FindItemsResults<Appointment> appointments = calendar.FindAppointments(cView);
            EWSModel objModel = null;
            foreach (Appointment appItem in appointments)
            {
                appItem.Load();
                List<string> attendees = new List<string>();
                foreach (var emailItem in appItem.RequiredAttendees)
                {
                    attendees.Add(emailItem.Name);
                }
                objModel = new EWSModel();
                objModel.AppointmentTitle = appItem.Subject.ToString();
                objModel.Subject = appItem.Subject.ToString();
                objModel.Body = StripHTML(appItem.Body.Text).Replace("\r", "").Replace("\n", "");
                objModel.EmailAddress = attendees;
                objModel.Location = appItem.Location.ToString();
                objModel.StartDate = appItem.Start;
                objModel.EndDate = appItem.End;
                objCalItems.Add(objModel);
            }
        }
        catch (Exception err)
        {
            throw err;
        }

        return objCalItems;
    }
Mike L
  • 4,693
  • 5
  • 33
  • 52

2 Answers2

1

I found the answer by myself. Using the impersonation technique, will meet the requirement. First, enable impersonation access to the Admin user of Exchange. Refer the following link to do that. Then from your code you can set the impersonation(with Admin user) for any user to access that user's calendar details (of exchange), use the following code for reference

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
service.Credentials = new WebCredentials(username, password);
service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, [username whose detail is needed]); 

Please refer the following link for more details.

0

The first thing to consider is that being an Admin doesn't give you any rights to other users mailboxes so if you want to access another users Mailbox that Admin will need to be specifically granted access to that Mailbox using Add-MailboxPermissions. Once you have granted access (and waited for that change to take affect) you can just use the FolderId overload eg

        FolderId CalendarFolderId = new FolderId(WellKnownFolderName.Calendar,"MaiiboxtoAcces@domain.com");
        CalendarFolder calendar = CalendarFolder.Bind(service, CalendarFolderId, new PropertySet(BasePropertySet.FirstClassProperties));

Another option is give the Admin account access to impersonated the user you want to access see https://msdn.microsoft.com/en-us/library/bb204095(EXCHG.140).aspx and https://msdn.microsoft.com/en-us/library/office/dd633680(v=exchg.80).aspx

Cheers Glen

Glen Scales
  • 20,495
  • 1
  • 20
  • 23