Helper Method to Extract a Value from User Profile Service Application

Consider a case where you need to pull information from User profile service application depending upon the property you supply in function.

                                    introducting

As shown in above image suppose you need to have User Profile Picture,User Display name,Designation,Location,Full profile view address etc. etc. Here function GetUserProfile Property takes the parameters like Account Name and Property to extract.Simply supply those parameters and Enjoy!!!
  1. /// <summary>    
  2. /// Method to extract user profile property details    
  3. /// </summary>    
  4. /// <param name="AccountName">Account name of logged in User</param>    
  5. /// <param name="propertyName">Property name to extract</param>    
  6. /// <returns>a sting with property value</returns>    
  7. public string GetUserProfileProperty(string AccountName, string propertyName)    
  8. {    
  9.     string resultValue = string.Empty;    
  10.     try    
  11.     {    
  12.         SPSecurity.RunWithElevatedPrivileges(delegate()    
  13.         {    
  14.             SPServiceContext oSPServiceContext = SPServiceContext.GetContext(SPContext.Current.Site);    
  15.             UserProfileManager oUserProfileManager = new UserProfileManager(oSPServiceContext);    
  16.             UserProfile oUserProfile = oUserProfileManager.GetUserProfile(AccountName);    
  17.     
  18.             if (oUserProfile != null)    
  19.             {    
  20.                 resultValue = oUserProfile[propertyName].Value.ToString();    
  21.             }    
  22.                 
  23.         });    
  24.         return resultValue;    
  25.     }    
  26.     catch (Exception ex)    
  27.     {    
  28.         throw;    
  29.     }    
  30. }  

Happy SharePoint !!!