Wednesday, May 8, 2013

How to retrieve s user profile in SharePoint 2010

The below code will show how to retrieve a user profile using the SharePoint object model

The following references are required:
  • Microsoft.Office.Server
  • Microsoft.Office.Server.UseProfiles
  • Microsoft.SharePoint
  • System.Web  
        private UserProfiles.UserProfile GetCurrentUserProfile(SPUser currentUser, string siteCollectionUrl)
                using (var site = new SPSite(siteCollectionUrl))
                    var serverContext = SPServiceContext.GetContext(site);
                    var profileManager = new UserProfileManager(serverContext);
                    var userProfile = profileManager.GetUserProfile(currentUser.RawSid);

                    return userProfile != null ? userProfile : null;
            catch (Exception ex)
                // Handle error and write them to a log
                return null;

I hope this has helped you, please feel free to drop me a note if you have any questions.