Sharepoint 2010 show the user profile picture programatically

SharePoint has a User Information list which can accessed using the object model.
So how do you get the profile picture? well i am posting a little code snippet which will make your life easier. It is in server object model though.

SPuser _user = SPcontext.current.web.CurrentUser;

using (SPSite Site = new SPSite(“Site URL”))
{
using (SPWeb Web = Site.OpenWeb())
{

SPList UserInfoList = Web.Lists["User Information List"];

SPListItem UserItem = UserInfoList.GetItemById(_user.ID);

UserItem["Picture"] = "Some url"; //-> here you can set or get the Picture property which is a url.
}
}

Related Post : Programmatically get user profile picture using Client Object model SharePoint 2010

2 comments:

  1. public static string GetUserProfilePictureURL(string username)
    {
    string Url = string.Empty;

    SPServiceContext serviceContext = SPServiceContext.GetContext(SPContext.Current.Site);
    UserProfileManager upm = new UserProfileManager(serviceContext);
    if (upm.UserExists(username))
    {
    UserProfile profile = upm.GetUserProfile(username);
    if (profile != null)
    {
    Url = profile["PictureURL"].Value.ToString();
    }
    }

    //change it to the default if the user does not have an image specified
    if (string.IsNullOrEmpty(Url))
    Url = "/_layouts/images/unknown.png";

    return Url;
    }

    ReplyDelete

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer or anyone else. Should you have any questions or concerns please e-mail me at sharepointprogrammingblogger@gmail.com .

Copyright (c) 2010 @ myshaepointwork.blogspot.com. All rights are reserved.Do Not Copy.

@ Learning SharePoint.com