0

To retrieve user profiles using server object model I have used UserProfileManager in C# code.

UserProfileManager userProfileMgr = new UserProfileManager(serviceContext);
IEnumerator userProfiles = userProfileMgr.GetEnumerator();
while (userProfiles.MoveNext())
{
 UserProfile userProfile = (UserProfile)userProfiles.Current;
 Console.WriteLine(userProfile.AccountName);
}

So now I'm trying to retrieve all user profiles from Office 365 site.

So how can I retrieve users profiles in SharePoint online app using JSOM?

3

Try the below code:

//Get User Profile Property for Site Users 
function getUsersDetails(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 personName = peopleManager.getUserProfilePropertyFor(user.get_loginName(),'AccountName');
           personsProperties.push(personName);
       }

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

    },
    Error);
}

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

Reference - How to get with SharePoint client APIs all user profiles?

However, if you are fan of REST API like me, you can use it as below:

$.ajax({
    url: "https://sitecollectionurl/_api/search/query?querytext='*'&sourceid='B09A7990-05EA-4AF9-81EF-EDFAB16C4E31'&selectproperties='AccountName'&rowlimit='500'",
    method: "GET",
    headers: { "Accept": "application/json; odata=verbose" },
    success: function (data) {
        console.log(data);
    },
    error: function (data) {
        console.log(data);
    }
}); 

Here, i am using the search rest api with people results source to get data from search instead of directly query the user profiles.

If you are using this approach, then Sharepoint search has a default "rowlimit" of 500, so you need to do pagination to get more than 500 profiles as below:

SharePoint REST API call with more than 500 rows

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.