使用WSS对象模型从SPUser获取用户照片

时间:2008-09-14 13:48:00

标签: sharepoint photo wss

我正在尝试通过WSS 3.0对象模型在Sharepoint的用户照片上检索用户。我一直在浏览网页寻求解决方案,但到目前为止,我一直无法找到办法。是否可能,如果是这样的话?

3 个答案:

答案 0 :(得分:5)

这是一个代码片段,可以帮助您完成工作。您可能需要进行一些额外的验证以避免任何异常(确保配置文件实际存在,确保图像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

答案 1 :(得分:3)

如果你严格地谈论WSS 3.0(而不是MOSS),那么你本身并没有全局用户配置文件,而是每个网站集中都有一个隐藏的用户信息列表。这意味着您无法使用Microsoft.Office.Server命名空间中的任何内容。

但是,只要您知道用户图片的URL,就可以以编程方式更新用户信息列表。只要您使用某种提升的权限运行,您就可以像使用任何其他SharePoint列表一样manipulate this list。请记住,此列表仅适用于网站集的范围,因此用户必须在整个地方进行相同的更新才能实际拥有照片网址。在有人向他们分配某种权限之前,用户不会进入用户信息列表,因此并非您域中的每个用户都在那里。

处理这个问题的干净方法绝对是用户配置文件机制是MOSS,但如果这是一个选项,那么问题应该真正更新以询问MOSS vs WSS。

答案 2 :(得分:2)

啊,你必须使用UserProfileManager类。 更多信息请访问:http://msdn.microsoft.com/en-us/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}
    //
}