user3080110
user3080110

Reputation: 127

how to get the calendar items from other users with EWS API Outlook

I like to get an overview of someone else calendar. When I use the method 1 I can get his calendar info except the 'Occurences' items are not in...

With method 2, I'm able to get the occurences items of that person, but it shows MY Calendar items instead of that person, while I pass in the service of that persons email address...

In both way I'm passing the " service " parameter which contains otherones email address...

I need all calendar info of the X person + the occurence... Why is method 2 giving my calendar items instead of that person ?

Any advice?...

Method 1: method-> Folder.Bind is used

string username ="[email protected]"
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
service.UseDefaultCredentials = true;
folderIdFromCalendar = new FolderId(WellKnownFolderName.Calendar, username);
PropertySet propertySet = new PropertySet(AppointmentSchema.Subject);
Folder TargetFolder = Folder.Bind(service, folderIdFromCalendar, propertySet);

MEthod 2: method -> FindAppointments is used.

  service.AutodiscoverUrl(username, RedirectionUrlValidationCallback);
                /////////

                DateTime startDate = DateTime.Now;
                DateTime endDate = startDate.AddDays(30);
                const int NUM_APPTS = 5;

                // Initialize the calendar folder object with only the folder ID. 
                CalendarFolder calendar = CalendarFolder.Bind(service, WellKnownFolderName.Calendar, new PropertySet());

                // Set the start and end time and number of appointments to retrieve.
                CalendarView ccView = new CalendarView(startDate, endDate, NUM_APPTS);

                // Limit the properties returned to the appointment's subject, start time, and end time.
                ccView.PropertySet = new PropertySet(AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.End);

                // Retrieve a collection of appointments by using the calendar view.
                FindItemsResults<Appointment> appointments2 = calendar.FindAppointments(ccView);

                Console.WriteLine("\nThe first " + NUM_APPTS + " appointments on your calendar from " + startDate.Date.ToShortDateString() +
                                  " to " + endDate.Date.ToShortDateString() + " are: \n");

                foreach (Appointment a in appointments2)
                {
                    Console.Write("Subject: " + a.Subject.ToString() + " ");
                    Console.Write("Start: " + a.Start.ToString() + " ");
                    Console.Write("End: " + a.End.ToString());
                    Console.WriteLine();
                }

Upvotes: 0

Views: 4257

Answers (2)

ThangLeQuoc
ThangLeQuoc

Reputation: 3073

I came here with the solution for Java with the ews-java-api library. The syntax for Java is pretty much close to C#, so I'm sharing the code snippet anyway.

Approach 1 - Binding to the calendar folder of the target user

This approach bind the calendar folder of the target user folder and execute the appointment query. Similar to the practical usecase where you are logged in as user A, and opening the calendar listing of user B.

import microsoft.exchange.webservices.data.core.ExchangeService;
import microsoft.exchange.webservices.data.core.enumeration.misc.ConnectingIdType;
import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
import microsoft.exchange.webservices.data.core.enumeration.property.WellKnownFolderName;
import microsoft.exchange.webservices.data.core.service.folder.CalendarFolder;
import microsoft.exchange.webservices.data.core.service.item.Appointment;
import microsoft.exchange.webservices.data.credential.ExchangeCredentials;
import microsoft.exchange.webservices.data.credential.WebCredentials;
import microsoft.exchange.webservices.data.misc.ImpersonatedUserId;
import microsoft.exchange.webservices.data.property.complex.FolderId;
import microsoft.exchange.webservices.data.property.complex.Mailbox;
import microsoft.exchange.webservices.data.search.CalendarView;
import microsoft.exchange.webservices.data.search.FindItemsResults;


/**
* Get user appointment within range
* @param targetUserEmail: Target User Email, e.g: "[email protected]"
* @param from From range
* @param to To range
* */
public List<Appointment> getAppointments(String targetUserEmail, Date from, Date to) throws Exception {
  // Initiate the exchange servive
  ExchangeCredentials techUserCredential = new WebCredentials("ewsTechUserName", "ewsTechPswd", "mydomain");
  exchangeService = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
  exchangeService.setCredentials(techUserCredential);
  exchangeService.setUrl(new URI("https://outlook.mycompany.ch/EWS/Exchange.asmx"));

  // binding to the calendar folder of that user
  FolderId targetUserCalendarId = new FolderId(WellKnownFolderName.Calendar, Mailbox.getMailboxFromString(targetUserEmail));
  CalendarFolder calendarFolder = CalendarFolder.bind(exchangeService, targetUserCalendarId);

  // Invoke the CalendarFolder#findAppointments method within range by from-to parameters
  FindItemsResults<Appointment> findResults = calendarFolder.findAppointments(new CalendarView(from, to));
  List<Appointment> results = findResults.getItems();
  return results;
}

Approach 2 - Direct binding to the calendar folder of the target user with Impersonation mode

This approach allow the credential account to impersonate the target user. With impersonation, it is like user B (the target user that we're browsing the entries) is viewing his own calendar entries. This might come handy if it's required to read information of the target user's private appointments.
Note that this approach requires the credential account need to be granted ApplicationImpersonation role by an Exchange server administrator.

import microsoft.exchange.webservices.data.core.ExchangeService;
import microsoft.exchange.webservices.data.core.enumeration.misc.ConnectingIdType;
import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
import microsoft.exchange.webservices.data.core.enumeration.property.WellKnownFolderName;
import microsoft.exchange.webservices.data.core.service.folder.CalendarFolder;
import microsoft.exchange.webservices.data.core.service.item.Appointment;
import microsoft.exchange.webservices.data.credential.ExchangeCredentials;
import microsoft.exchange.webservices.data.credential.WebCredentials;
import microsoft.exchange.webservices.data.misc.ImpersonatedUserId;
import microsoft.exchange.webservices.data.property.complex.FolderId;
import microsoft.exchange.webservices.data.property.complex.Mailbox;
import microsoft.exchange.webservices.data.search.CalendarView;
import microsoft.exchange.webservices.data.search.FindItemsResults;

/**
* Get user appointment within range
* @param targetUserEmail: Target User Email, e.g: [email protected]
* @param from From range
* @param to To range
* */
public List<Appointment> getAppointments(String targetUserEmail, Date from, Date to) throws Exception {
  // Initiate the exchange servive
  ExchangeCredentials techUserCredential = new WebCredentials("ewsTechUserName", "ewsTechPswd", "mydomain");
  exchangeService = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
  exchangeService.setCredentials(techUserCredential);
  exchangeService.setUrl(new URI("https://outlook.mycompany.ch/EWS/Exchange.asmx"));

  /* This impersonation technique is required, if we also need to load the meeting detail if the current user is viewing his own
    * private calendar entry */
  ImpersonatedUserId impersonateUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, targetUserEmail);
  exchangeService.setImpersonatedUserId(impersonateUserId);

  // binding to the calendar folder of that user
  FolderId targetUserCalendarId = new FolderId(WellKnownFolderName.Calendar, Mailbox.getMailboxFromString(targetUserEmail));
  CalendarFolder calendarFolder = CalendarFolder.bind(exchangeService, targetUserCalendarId);

  // Invoke the CalendarFolder#findAppointments method within range by from-to parameters
  FindItemsResults<Appointment> findResults = calendarFolder.findAppointments(new CalendarView(from, to));
  List<Appointment> results = findResults.getItems();
  return results;
}

Upvotes: 0

Glen Scales
Glen Scales

Reputation: 22032

Method 2 should work but you need to use the FolderId's overload to specify the Mailbox to access so change

CalendarFolder calendar = CalendarFolder.Bind(service, WellKnownFolderName.Calendar, new PropertySet());

to

folderIdFromCalendar = new FolderId(WellKnownFolderName.Calendar, username);
CalendarFolder calendar = CalendarFolder.Bind(service, folderIdFromCalendar , new PropertySet());

Upvotes: 1

Related Questions