EWS – How to fix The “UserId in the folder permission at index [xx] is invalid”

This article specifically address a problem I found when trying to disable archiving on specific folders in Exchange by following Prevent Notes, Calendar and Tasks from getting archived by using EWS script. When running the script I would get the error The UserId in the folder permission at index [xx] is invalid. The rest of this article will assume you have read the linked post and understand it. The Cause The script works by downloading the settings of a user's calendar and adding a "no archive" personal tag to it, it then syncs these changes back to the online version of the folder. Where the problems starts is with the download of the folder; it doesn't just download the folder name, it downloads delegate permissions as well. In my case it wasn't downloading these permissions correctly so I had some blank entries (more on that later). After adding the archive tag it then tries to save the folder settings including the blank permissions resulting in the above error. The fix The above script downloads the user's calendar by calling the bind method on line 50. The default bind command downloads permissions as well. All we need to do is exclude them from