2

How to get the users image in my site programatically?

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

Vedran Rasol
  • 8,768
  • 26
  • 52
Nikhil J
  • 4,934
  • 10
  • 46
  • 91

2 Answers2

7

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];
Vedran Rasol
  • 8,768
  • 26
  • 52
Jorge Carvalho
  • 766
  • 6
  • 27
0
  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;
Mike
  • 12,186
  • 8
  • 41
  • 64
Nikhil J
  • 4,934
  • 10
  • 46
  • 91
  • 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 06 '11 at 00:40