I have a requirement where I am checking a user account whether it does exist in User Profile Service or not. And if the account doesn't exist then it will create an account. I am using CSOM for this.

It throws an exception "Forbidden" on clientContext.ExecuteQuery() when I don't impersonate the identity and the account doesn't exist. By impersonating the identity the account gets created even for those user whose accounts are already existent which I don't prefer. I am using the following code:

var loginName = HttpContext.Current.User.Identity.Name;
var peopleManager = new PeopleManager(clientContext);
var properties = peopleManager.GetPropertiesFor(loginName);
clientContext.Load(properties, p => p.PersonalUrl, p => p.UserProfileProperties);

Is there a way where I can catch UserProfileNoUserFoundException exception instead of exception "Forbidden"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.