How do I get only the new/updated events from a calendar in Office365?

427 Views Asked by At

I'm a junior developer trying to figure out the Outlook365 REST API.

I'm using the office365 rest api to sync my events from my calendar. On my first request, I get all the events from a time range. This returns a deltaToken. After that I add more events to my calendar and make another call using the last deltaToken returned from my calls. This request returns all the events (including the new ones) and 2 deltaTokens (the first is the one I've sent, and the second is a new one). I only want to get the new events, not all of them. If I'm making the request using the deltaToken from my previous request, does that not mean that I've already synced all my previous events and shouldn't the next calls get only the new / modified events?

Below is a response. Is it ok to have 2 deltaTokens in my response?

{"@odata.context":"https://outlook.office.com/api/beta/$metadata#Me/CalendarView, "value":[list of events], "@odata.deltaLink":"https://outlook.office.com/api/beta/Me/CalendarView/?startDateTime=2017-07-25T00%3a00%3a00Z&endDateTime=2017-09-01T00%3a00%3a00Z&deltatoken=GGXXX&%24deltatoken=GWXXX" }

1

There are 1 best solutions below

3
On BEST ANSWER

It sounds like you are not finalizing your first sync. The way sync works in the API is a little counter-intuitive. It isn't as simple as checking for a deltaToken to see if you're done.

The reason is that the first request with Prefer: odata.track-changes always returns a deltaToken, even if there are more results. The docs give more detail here, but the general sequence is:

  1. Do GET on the calendar view with Prefer: odata.track-changes.
  2. Verify that Preference-Applied: odata.track-changes header is present in response. If so, get the deltaLink and do a GET on that.
  3. Now enter your loop. If you get a nextLink in response, do a GET on that and repeat. If you get a deltaLink, the sync is done. Store the value until your next sync.