SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to understand why the following error occurs. It seems to me that this fails whent the _userName does not have a My Site. I have seen that this code works in environments where My Sites are enabled and the My Site exists for that user. On environments where My Sites are not enabled the error occurs. Thanks for your help.

using (SPSite site = SPContext.Current.Site)
   SPServiceContext context = SPServiceContext.GetContext(site);
   profileManager = new UserProfileManager(context);

   user = profileManager.GetUserProfile(_userName);

Error Message:

An error was encountered while retrieving the user profile.
at Microsoft.Office.Server.UserProfiles.UserProfileCache.GetUserData(UserProfileManager objManager, Nullable`1 recordId, Guid gAcct, String strAcct, Byte[] bSid, String strEmail, Boolean doNotResolveToMasterAccount) 
at Microsoft.Office.Server.UserProfiles.UserProfile.RetrieveUser(String strAcct, Guid gAcct, Byte[] bSid, Nullable`1 recordId, Boolean doNotResolveToMasterAccount, Boolean loadFullProfile) 
at Microsoft.Office.Server.UserProfiles.UserProfile..ctor(UserProfileManager objManager, String strAcct, Boolean doNotResolveToMasterAccount, Boolean forceUserIsSelf, Boolean loadFullProfile) 
at Microsoft.Office.Server.UserProfiles.UserProfileManager.GetUserProfile(String strAccountName, Boolean doNotResolveToMasterAccount, Boolean loadFullProfile) 
at Microsoft.Office.Server.UserProfiles.UserProfileManager.GetUserProfile(String strAccountName, Boolean doNotResolveToMasterAccount) 
at Microsoft.Office.Server.UserProfiles.UserProfileManager.GetUserProfile(String strAccountName) 
at MyWepPart.MyWepPartWebPart.MyWepPartWebPartUserControl.<>c__DisplayClass2.<_getDistinguishedNameFromUPS>b__0() 
at Microsoft.SharePoint.SPSecurity.<>c__DisplayClass4.b__2() at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode) 
at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param) 
at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode) 
at MyWepPart.MyWepPartWebPart.MyWepPartWebPartUserControl._getDistinguishedNameFromUPS(Boolean& hasErrors)
share|improve this question
What error are you getting? – Dave Wise Oct 13 '11 at 19:13
Mr. Wise, It is posted in the original message. Thanks for viewing. – ChiliYago Oct 13 '11 at 20:38
You don't need to run with elevated privs to load a user's profile. Are you passing a valid username in the _username variable? Does a profile exist for the user? – Mike Oryszak Oct 13 '11 at 21:49
I was asking about an actual error, not the generic one posted. If you check the /LOGS folder when this happens you should be able to get a more specific error message. – Dave Wise Oct 13 '11 at 22:50
up vote 3 down vote accepted

You should check if the user exists with before you ask for the profile:

if ( profileManager.UserExists(_userName) )
    user = profileManager.GetUserProfile(_userName); 

It might also be a good idea to check if the login is valid (eg. the user has logged in before). You can use EnsureUser() for this

SPUser user = site.RootWeb.EnsureUser(_userName);

and then use user.LoginName to get the profile.

share|improve this answer
The EnsureUser method helped out a lot. In my case the complexity resulted because the username was using a domain alias which differs from domain name imported by the User Profile service. So I had to change the domain name prefix of the User's login name to the one imported by the UPS. – ChiliYago Oct 14 '11 at 18:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.