Wednesday 23 April 2014

SharePoint Get User Name From Profile

SharePoint Get User Name From Profile
GetUserNameFromProfile(HttpContext.Current.User.Identity.Name)
public String GetUserNameFromProfile(String accountId)
{
try
{
String preferredName = String.Empty;
SPSite site = SPContext.Current.Site;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite siteColl = new SPSite(site.ID);
ServerContext context = ServerContext.GetContext(siteColl);
UserProfileManager profileManager = new UserProfileManager(context);
UserProfile currentUser = profileManager.GetUserProfile(accountId);
preferredName = currentUser[PropertyConstants.PreferredName].Value.ToString();
siteColl.Dispose();
});
return preferredName;
}
catch (SPException exp)
{
throw exp;
}
}