Friday, August 16, 2013

Get User Profile properties in SharePoint

User Profile Service application maintains User Profiles for all the Users in SharePoint and synchronizes with the Active Directory or with your Custom DB based on the configuration. If you want to access the User Profile properties from Web Application make sure your Web Application is configured with any of the User Profile Service application.

Check here to see Creation of User Profile Service application.
Follow below steps to check the Service connections.
1.       Navigate to Central Administration à Application Management à Manage Web applications
2.       Select your Web Application and Click on the Service Connections from Ribbon.

 
3.       Make sure User Profile Service application is selected from the list and click OK. 

To access a specific user profile, we need call the UserProfileManager class to create an object  for the UserProfile and to get the Properties of the User from the User Profile Database.

   
SPSecurity.RunWithElevatedPrivileges(delegate()
 {
    using (SPSite site = new SPSite(web.Url))
    {
      using (SPWeb objWeb = site.OpenWeb())
      {
         string strDisplayName = string.Empty;
         string strMySiteURL = string.Empty;
         string strEmail = string.Empty;
         SPServiceContext sc = SPServiceContext.GetContext(web.Site);
 
         //Get the User Profile Service configured to the current site
         UserProfileManager objUserProfileManager = new UserProfileManager(sc);
         UserProfile profile = null;
                           
         //Check whether the specified User exists in the UserProfile
         if (objUserProfileManager.UserExists(accountName))
         {
             //Get the User Profile using the Profile Manager
             profile = objUserProfileManager.GetUserProfile(accountName);
           
             //Now you can get all the Properties of the User from this object
             strDisplayName = profile.DisplayName;
             strMySiteURL = profile.PublicUrl.ToString();
             strEmail = profile[“WorkEmail”].Value();
         }
      }
    }                           
 });
            
 

If you need full access for all the User Profiles, you need to have “Manage User Profile” rights. By default, everyone will have read access to all the User Profiles.
Related Posts Plugin for WordPress, Blogger...