Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

How to get the users image in my site programatically?

I need to show image of the logged-in user in my webpart.

share|improve this question
add comment

2 Answers 2

up vote 7 down vote accepted

The function below will get you the UserProfile based on the accountName

public static 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;
}

The picture URL will be in

userProfile[PropertyConstants.PictureUrl];
share|improve this answer
add comment
  SPFieldUrlValue fieldUrl = new SPFieldUrlValue(item["Links"].ToString());
  url = fieldUrl.Url;
  string[] splittedValues = url.Split('=');
  string accountName =splittedValues[1].ToString();
  accountName.Replace("%5C","\\");

  ServerContext ServerContext = ServerContext.GetContext(site);
  UserProfileManager profileManager = new UserProfileManager(ServerContext);
  UserProfile spUser = profileManager.GetUserProfile(accountName);
  IDictionaryEnumerator userProperties = spUser.GetEnumerator();

  string picture = spUser["PictureURL"].Value.ToString();
  row["Links"] = url;
  row["URL"] = picture;
share|improve this answer
1  
Do you always have the user account on the url? Why split strings? If you want info on current user, wouldn't it be cheaper to just get user info from SPContext.Current.Web.CurrentUser? –  Jorge Carvalho Oct 6 '11 at 0:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.