如何使用SharePoint客户端API获取所有用户配置文件?

时间:2014-02-04 09:19:30

标签: sharepoint sharepoint-2013

为了实现生日的SharePoint 2013应用程序,我需要从网站集中获取所有用户配置文件。为此,我想使用(或多个)客户端API。请参阅http://msdn.microsoft.com/en-us/library/jj163800.aspx#bkmk_APIversions

不幸的是,我在API描述中找不到相当于Microsoft.Office.Server.UserProfiles的内容。有Microsoft.SharePoint.Client.UserProfiles.PeopleManager种方法GetUserProfilePropertiesForGetUserProfilePropertyFor,只有一个用户个人资料。

所以我的问题是:如何使用CSOM,JSOM,REST(或任何客户端技术)获取网站集中的所有用户配置文件?

2 个答案:

答案 0 :(得分:4)

由于CSOM提供与人每用户范围相关的操作方法,因此您可以先使用SP.Web.siteUsers property检索所有网站用户。然后使用 SP.UserProfiles.PeopleManager.getUserProfilePropertyFor Method获取BirthDay财产,如下所示:

//Get Birthday User Profile Property for Site Users 
function getUsersBirthdays(Success,Error) {
    var clientContext = new SP.ClientContext.get_current();
    var web = clientContext.get_web(); 

    var users = web.get_siteUsers();
    clientContext.load(users);
    clientContext.executeQueryAsync(
    function() {
       var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
       var personsProperties = [];
       for(var i = 0; i < users.get_count();i++)
       {
           var user = users.getItemAtIndex(i);
           var personBirthday = peopleManager.getUserProfilePropertyFor(user.get_loginName(),'SPS-Birthday');
           personsProperties.push(personBirthday);
       }

       clientContext.executeQueryAsync(
           function() {
             Success(personsProperties);
           },
           Error);

    },
    Error);



}



//Usage
var scriptbase = _spPageContextInfo.webAbsoluteUrl + '/_layouts/15/';
$.getScript(scriptbase + 'SP.js', function () {
  $.getScript(scriptbase + 'SP.UserProfiles.js', function () {
    getUsersBirthdays(function(usersProperties){
       for(var i = 0; i < usersProperties.length;i++)
       {
           console.log(usersProperties[i].get_value());
       }
    },
    function(sender,args){
       console.log(args.get_message());
    });
  });
});  

答案 1 :(得分:1)

This也适用于SP2013

function GetUsersGroups(){

  ClientContext context = new Microsoft.SharePoint.Client.ClientContext("http://SPSite");

  GroupCollection groupCollection = context.Web.SiteGroups;
  context.Load(groupCollection,
  groups = > groups.Include(group = > group.Users));

  context.ExecuteQuery();

  foreach (Group group in groupCollection)
  {
    UserCollection userCollection = group.Users;

    foreach (User user in userCollection)
    {
      MessageBox.Show("User Name: " + user.Title + " Email: " + user.Email + " Login: " + user.LoginName);
    }
  }
  //Iterate the owners group
  Group ownerGroup = context.Web.AssociatedOwnerGroup;
  context.Load(ownerGroup);
  context.Load(ownerGroup.Users);
  context.ExecuteQuery();
  foreach (User ownerUser in ownerGroup.Users)
  {
    MessageBox.Show("User Name: " + ownerUser.Title + " Email: " + ownerUser.Email + " Login: " + ownerUser.LoginName);
  }
  context.Dispose();
}
相关问题