Tuesday, January 25, 2011


Introduction:
In this article we will discuss how to get user profile details by using SharePoint object model classes in SharePoint. In my last article we have discussed Save user profile properties programmatically in sharepoint 2010.
Description:
Here we will discuss how to get user profile properties and also how to access profile properties using SharePoint 2010. We have used here SharePoint object model.
private UserProfile GetUserInfo(string AccountName)
{
UserProfile profile = null;
SPServiceContext serviceContext = SPServiceContext.Current;
UserProfileManager profileManager = new UserProfileManager(serviceContext);
if (AccountName != string.Empty)
{
profile = profileManager.GetUserProfile(AccountName);
}
else
{
profile = profileManager.GetUserProfile(SPContext.Current.Web.CurrentUser.RawSid);
}
return profile;
}


void GetCurrentUserDetails()
{
UserProfile userProfile = GetUserInfo("");
lblUserName.Text = GetUserName(userProfile);
if (userProfile[PropertyConstants.FirstName].Value != null)
{
lblUserName.Text += " (" + userProfile[PropertyConstants.FirstName].Value.ToString() + ")";
}

try
{
imgBtnUserImage.ImageUrl = userProfile[PropertyConstants.PictureUrl].Value.ToString();
}
catch (Exception ex)
{
imgBtnUserImage.ImageUrl = "/_layouts/ExpressionsWP/0.jpg";
}
if (userProfile[PropertyConstants.Department].Value != null)
{
ltrAddress.Text = userProfile[PropertyConstants.Department].Value.ToString();
}
if (ltrAddress.Text != string.Empty)
{
if (userProfile[PropertyConstants.Location].Value != null)
{
ltrAddress.Text += "," + userProfile[PropertyConstants.Location].Value.ToString();
}
}
else
{
if (userProfile[PropertyConstants.Location].Value != null)
{
ltrAddress.Text += userProfile[PropertyConstants.Location].Value.ToString();
}
}
}
If any problem or error, please post it in the new SharePoint Forum.


4 Comments:

  1. I am getting following error when I use this code.

    No User Profile Application available to service the request. Contact your farm administrator.

    ReplyDelete
  2. Check the permission levels

    http://littletalk.wordpress.com/2010/12/09/no-user-profile-application-available-to-service-the-request/

    ReplyDelete
  3. Thanks for your immediate response. I already gone through this link but what is feel is it will not make any sense adding all your application users (more than 1000 users) and giving permissions on service application in central administration. My requirement is on home page one custom web part will show the log-in user's profile information which includes few custom properties also. In 2007 it's working fine but in 2010 it's giving problem.

    ReplyDelete
  4. Ho do I get the value of a custom user profile property with the above code

    ReplyDelete