Question

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);
}

SPSecurity.RunWithElevatedPrivileges(delegate()
{
   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)
Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top