Monday, October 8, 2012

Get current user Profile in SharePoint 2010

Earlier i have given a customised solution to get the current user permissions in share point 2010.Here i will show how to get the user details from share point site using  UserProfileManager class.Before going to get the profile details we need to check whether the user is valid or not.Then the resultant data will be assigned to user profile class and get account name into string

SPSite sps = SPContext.Current.Site;
using (SPWeb spw = sps.OpenWeb())
{
SPServiceContext spsc = SPServiceContext.GetContext(sps);
UserProfileManager uprofile = new UserProfileManager(spsc);
string useraccountname = SPContext.Current.Web.CurrentUser.LoginName;
if (uprofile.UserExists(useraccountname))
{
UserProfile Profileinfo = uprofile.GetUserProfile(useraccountname);
if (!string.IsNullOrEmpty(Profileinfo["FirstName"].ToString()))
{
string AccountName = userProfile["FirstName"].ToString();
}
}
}

No comments:

Bel