mbrb
mbrb

Reputation: 403

How to get with SharePoint client APIs all user profiles?

For implementing a birthday's SharePoint 2013 app I need to get all user profiles from a site collection. For this purpose I'd like to use a (or multiple) client API(s). See http://msdn.microsoft.com/en-us/library/jj163800.aspx#bkmk_APIversions.

Unfortunately I couldn't find in the APIs description an equivalent of Microsoft.Office.Server.UserProfiles. There are in Microsoft.SharePoint.Client.UserProfiles.PeopleManager two methods, GetUserProfilePropertiesFor and GetUserProfilePropertyFor, that only get a single user profile.

So my question is: how to get with CSOM, JSOM, REST (or any client side technology) all user profiles in site collection?

Upvotes: 2

Views: 21034

Answers (2)

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59358

Since CSOM provides methods for operations related to people per user scope, you could retrieve all site users first using SP.Web.siteUsers property. and then use SP.UserProfiles.PeopleManager.getUserProfilePropertyFor Method to get BirthDay property as demonstrated below:

//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());
    });
  });
});  

Upvotes: 4

Greg
Greg

Reputation: 944

This also should work for 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();
}

Upvotes: 1

Related Questions