Sunday, 3 June 2012

An error was encountered while retrieving the user profile in Sharepoint 2010

Hi,

An issue was reported to me where some of the developers were facing some issues in executing the code to get user profile in sharepoint 2010. The exception was following:

Microsoft.Office.Server.UserProfiles.UserNotFoundException was unhandled
  Message=An error was encountered while retrieving the user profile.
  Source=Microsoft.Office.Server.UserProfiles
  StackTrace:
       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 WelcomeControlModification.Program.Main(String[] args)

This exception was originating from this line of code:

UserProfile userProfile = manager.GetUserProfile("Domain\userName");

We checked the domain as well as the userName and found no issues with that. We even checked the permissions of the ID executing the code. Finally a small modification based on my experience with Sharepoint 2007 solved this issue we just replaced

UserProfile userProfile = manager.GetUserProfile("Domain\userName");

With

UserProfile userProfile = manager.GetUserProfile("Domain\\userName");

In short we just added an additional \ as a escape sequence.
This resolved our issue.

I hope this will help you out.

Thanks,
Rahul Rashu


No comments:

Post a Comment