views:

3880

answers:

3

I'm trying to retrieve a user on sharepoint's user photo through the WSS 3.0 object model. I've been browsing the web for solutions, but so far I've been unable to find a way to do it. Is it possible, and if so how?

+1  A: 

Ah, You have to use the UserProfileManager class. More information here: http://msdn.microsoft.com/en-us/library/microsoft.office.server.userprofiles.userprofilemanager.aspx

Example use:

public override void ItemAdded(SPItemEventProperties properties)
{
    // Get list item on which the event occurred.
    SPListItem item = properties.ListItem;

    // Set the Author Image field to the user's PictureURL if it exists.
    using (SPWeb web = properties.OpenWeb())
    {
        // Author: {C32DB804-FF2D-4656-A38A-B0394BA5C931}
        SPFieldUserValue authorValue = new SPFieldUserValue(properties.OpenWeb(), item[new Guid("{C32DB804-FF2D-4656-A38A-B0394BA5C931}")].ToString());

        UserProfileManager profileManager = new UserProfileManager(ServerContext.GetContext(web.Site));
        UserProfile profile = profileManager.GetUserProfile(authorValue.LookupId);
        UserProfileValueCollection values = profile[PropertyConstants.PictureUrl];

        if (values.Count > 0)
        {
            // Author Image: {37A5CA4C-7621-44d7-BF3B-583F742CE52F}
            SPFieldUrlValue urlValue = new SPFieldUrlValue(values.Value.ToString());
            item[new Guid("{37A5CA4C-7621-44d7-BF3B-583F742CE52F}")] = urlValue.Url;
        }
    }

    item.Update();

    // News Text: {7F55A8F0-4555-46BC-B24C-222240B862AF}
    //

    // Author Image: {37A5CA4C-7621-44d7-BF3B-583F742CE52F}
    // 

    // Publish Date: {45E84B8B-E161-46C6-AD51-27A42E4992B5}
    //
}
+3  A: 

Here is a code snippet that should help get the job done for you. You may need to do some additional validation to avoid any exceptions (ensuring the profile actually exists, ensuring the image URL actually exists, etc...):

    //get current profile manager
    UserProfileManager objUserProfileManager = new UserProfileManager(PortalContext.Current);
    //get current users profile
    UserProfile profile = objUserProfileManager.GetUserProfile(true);
    //get user image URL
    string imageUrl = (string)profile[PropertyConstants.PictureUrl];

    //do something here with imageUrl
spoon16
profile[PropertyContants.PictureURL] returns a collection so you will need to get the value from that since you can't cast it directly to a string.
This will only work for MOSS - WSS on its own does not contain the profile manager service or class.
Matt Clark
+1  A: 

If you are strictly talking about WSS 3.0 (and not MOSS), then you really don't have global user profiles per se, but a hiddenh User Information List in each site collection. That mean none of the stuff in the Microsoft.Office.Server namespaces is available to you.

However, you can update the User Information List programatically as long as you know the URL to a user's picture. As long as you're running with some kind of elevated privileges, you should be able to manipulate this list just like you can with any other SharePoint list. Keep in mind that this list is only good for the scope of a site collection, so users would have to make this same update all over the place to actually have a photo URL. Plus users don't get into the User Information List until someone assigns some kind of permission to them, so not every user in your domain will be in there.

The clean way to handle this is definitely the User Profile mechanism is MOSS, but if that's an option the question should really be updated to ask about MOSS vs WSS.

Sam Yates