使用WSS对象模型从SPUser获取用户照片
时间:2020-03-05 18:52:58 来源:igfitidea点击:
我正在尝试通过WSS 3.0对象模型在Sharepoint的用户照片上检索一个用户。我一直在浏览Web上的解决方案,但到目前为止,我一直找不到找到解决方案的方法。有可能吗?
解决方案
回答
嗯,我们必须使用UserProfileManager类。
此处的更多信息:http://msdn.microsoft.com/zh-cn/library/microsoft.office.server.userprofiles.userprofilemanager.aspx
使用示例:
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} // }
回答
这是一个代码片段,应该可以完成工作。我们可能需要做一些添加的验证以避免任何异常(确保配置文件确实存在,确保图像URL实际上存在,等等...):
//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
回答
如果我们严格讲的是WSS 3.0(而不是MOSS),那么我们实际上本身就没有全局用户配置文件,但是每个网站集中都有一个隐藏的用户信息列表。这意味着我们无法使用Microsoft.Office.Server命名空间中的所有内容。
但是,只要知道用户图片的URL,就可以以编程方式更新"用户信息列表"。只要我们以某种提升的特权运行,就应该能够像使用其他任何SharePoint列表一样操作此列表。请记住,此列表仅适用于网站集的范围,因此用户必须在整个地方进行相同的更新才能真正拥有照片URL。另外,除非有人向他们分配某种权限,否则用户不会进入"用户信息列表",因此并非我们域中的每个用户都将位于其中。
解决此问题的干净方法肯定是用户配置文件机制是MOSS,但是如果可以选择,则应该对问题进行真正的更新以询问有关MOSS vs WSS的问题。