如何在sharepoint 2013中通过他们的登录名获取用户个人资料的网址?

时间:2014-04-03 09:12:47

标签: c# sharepoint

获得此存储库代码。

public IEnumerable<FollowedPerson> ReadFollowedPersonsById(int id, SPList list)
{
    var item = list.GetItemById(id);
    if (item == null)
    {
        return null;
    }
    var likedByCollection = (SPFieldUserValueCollection)item["LikedBy"];
    if (likedByCollection == null)
    {
        return null;
    }
    var followers = new Collection<FollowedPerson>();
    foreach (var liker in likedByCollection)
    {
        followers.Add(new FollowedPerson
            {
                Name = liker.User.Name,
                ImageUrl = string.Empty //TODO Urls of users avatars
            });
    }
    return followers;
} 

我想获得喜欢某个列表项的用户集合。收集项目如下所示:

public class FollowedPerson
{
    public string Name { get; set; }

    public string ImageUrl { get; set; }
}

让他们的名字正常工作,但我不知道如何通过他们的登录名获得他们的头像。

liker.User //does not contain any url of user`s image

1 个答案:

答案 0 :(得分:1)

您可以从用户个人资料中获取此信息。您可以在此处了解如何使用它:Work with user profiles in SharePoint 2013。 有用于检索所有用户配置文件属性的代码段:

// Replace the following placeholder values with the target SharePoint site and
// target user.
const string serverUrl = "http://serverName/";  
const string targetUser = "domainName\\userName";  

// Connect to the client context.
ClientContext clientContext = new ClientContext(serverUrl);

// Get the PeopleManager object and then get the target user's properties.
PeopleManager peopleManager = new PeopleManager(clientContext);
PersonProperties personProperties = peopleManager.GetPropertiesFor(targetUser);

// Load the request and run it on the server.
// This example requests only the AccountName and UserProfileProperties
// properties of the personProperties object.
clientContext.Load(personProperties, p => p.AccountName, p => p.UserProfileProperties);
clientContext.ExecuteQuery();

foreach (var property in personProperties.UserProfileProperties)
{
     Console.WriteLine(string.Format("{0}: {1}", 
         property.Key.ToString(), property.Value.ToString()));
}