Graph API group delta changes (contd.)

I had to conclude the previous post abruptly. Thinking about it later, I realized I hadn’t shown any proper examples and such. So here goes.

I have a source group with 3 members. The group Id is “83e46fb6-9557-416c-97c4-2b960f0be57a”. I have a destination group that’s currently empty. Its id is “98f792dd-bb9f-4245-b927-7300993c8e8d”. I want to sync (one way) from the source to the destination.

The beauty of the delta API, like I alluded to earlier, is that the first request gives you a list of members as additions. Subsequent delta requests continue to give both additions and deletions. So one can use the same code to do both the initial sync and further delta syncs.

Here’s the previous script modified to do just that:

The output looks like this:

And now the destination group has these members:

Let me add someone to the source group now.

And let’s see if that shows up if I query the delta link:

Yessir, it does!

There is a small problem though. Previously my code was doing:

But that’s not going to work coz there is no @odata.nextLink to begin with.

This is something I should be wary of with the first delta link call too, I suppose. Because in my testing I always got a @odata.nextLink I just kept looping until that was none; but I wonder if that’s always the case. Instead, what I should really focus on is @odata.deltaLinkand also keep looping until @odata.deltaLink is not null.

Hmm. Here goes:

This works:

I should rewrite the code a bit so it can handle both initial invocation as well as subsequent delta invocations. I can do this based on whether I have a delta URL stored or not. Also, there is a bug in the above in that I am not capturing @odata.nextLink if that exists – which is possible if I have more than one page of results.

Here’s the final version.

Destination changes

Here’s the two groups as of now:

They are identical. Now I remove someone from the destination group.

Will this person get added on the next run? An easy to check that is to run the delta Link to see if it shows any additions.

No surprises there, it does not. So this way we can add/ remove users to the destination and they won’t be affected by the sync. Of course, if I remove someone in the destination group but also remove and add them on the source side then they will get added back.