Getting user photos from SPUser using the WSS object model

I am trying to upload a user to a Sharepoint user photo through the WSS 3.0 object model. I was browsing the Internet for solutions, but so far I have not been able to find a way to do this. Is it possible, and if so, how?

+4
source share
3 answers

Here is a snippet of code that should help complete the task for you. You may need to do extra checking to avoid any exceptions (to make sure the profile really exists, make sure the image URL really 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 
+5
source

If you are strictly talking about WSS 3.0 (and not MOSS), then you really do not have global user profiles as such, but a hidden list of user data in each site collection. This means that none of the material in the Microsoft.Office.Server namespaces is available to you.

However, you can update the list of user information programmatically if you know the URL for the user image. While you are using some kind of elevated privilege, you should manipulate this list in the same way as with any other SharePoint list. Keep in mind that this list is only for the site collection area, so users will need to do the same update everywhere to actually have the URL of the photo. Plus, users do not get into the list of user information until someone assigns them some permission, so not every user in your domain will be there.

The clean way to deal with this is, of course, the user profile mechanism - this is MOSS, but if you really need to update this parameter to ask about MOSS and WSS.

+3
source

Ah, you should use the UserProfileManager class. Additional information here: http://msdn.microsoft.com/en-us/library/microsoft.office.server.userprofiles.userprofilemanager.aspx

Usage example:

 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 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} // } 
+2
source

Source: https://habr.com/ru/post/1276679/


All Articles