I was recently working on a SharePoint Online project where we were trying to automate library creation and provide required permissions on those libraries. We had an issue while modifying permissions with CSOM code on SharePoint libraries when the Created By user had left the company.
In this post I will outline the cause and the resolution as there was no online reference for resolving this error.
Issue: The CSOM code was throwing an error “User not found” even when creating a User object from web.EnsureUser() method.
Cause: The User object returned by web.EnsureUser() method was empty but not null and hence couldn’t be instantiated while adding after breaking permissions.
Resolution: The resolution to this issue was to explicitly load of the user object, then catch the exception while loading, and set a flag to false which could be later be checked to prevent the add method from erroring out. Yeah, this is a roundabout way of overcoming the issue but it works. Hopefully it will save you some hours.
Below is the code that could be used to do that.

Category:
Application Development and Integration, Azure Platform, SharePoint, Uncategorized