I am trying to access a user profile property in a list item event event receiver(item adding).
But I get the following error
The user could not be found in the database. You must first create this user by calling CreateUserProfile or GetUserProfile(true).
I have checked the user profile service for the user and the user has a profile in service, I also have checked the profile existence in code using
userProfileManager.UserExists(accountName) and the profile exist
but when I want to access it I get that error
Below is the code
SPServiceContext spServiceContext = SPServiceContext.GetContext(spSite); UserProfileManager userProfileManager = new UserProfileManager(spServiceContext); if (!userProfileManager.UserExists(accountName)) return string.Empty; UserProfile userProfile = userProfileManager.GetUserProfile(false); var retrunValue = userProfile[strKey].Value ?? string.Empty; return retrunValue.ToString();