Search code examples
sharepoint-2013sharepoint-onlinesharepoint-userprofilesharepoint-jsom

Sharepoint Online Add-In JSOM Fetch All Users too long


I created app in SharePoint online where I get all users with a list of their properties (http://social.technet.microsoft.com/wiki/contents/articles/25074.sharepoint-online-working-with-people-search-and-user-profiles.aspx).

the app works correctly, but the result page (350 users) load time is very long (~15 seconds).

Apparently, the main wait time - this is sending POST request and receiving a response (~10 seconds).

How can I optimize this app?

'use strict';

(function ($) {

$(document).ready(function () {
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
        SP.SOD.executeFunc('SP.UserProfiles.js', 'SP.UserProfiles', getAllUsers);
    });
});

var users = [];    
var userProfileProperties = [];
var userProperties = [];
var userPropertiesFor = [];
var searchTerm = '*';
var results;


function getAllUsers() {

    var clientContext = new SP.ClientContext.get_current();
    var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(clientContext);
    keywordQuery.set_queryText(searchTerm);        
    keywordQuery.set_sourceId("B09A7990-05EA-4AF9-81EF-EDFAB16C4E31");
    keywordQuery.set_rowLimit(500);
    keywordQuery.set_trimDuplicates(false);

    var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(clientContext);
    results = searchExecutor.executeQuery(keywordQuery);
    clientContext.executeQueryAsync(onQuerySuccess, onQueryError);
}

function onQueryError(sender, args) {
    alert(args.get_message());
}


function onQuerySuccess() {        
    $.each(results.m_value.ResultTables[0].ResultRows, function () {
        users.push(this.AccountName);            
    });        
    fetchProfilePropertiesForUsers();
}

function fetchProfilePropertiesForUsers() {        
    var clientContext = new SP.ClientContext.get_current();
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
    var profilePropertyNames = ["PreferredName", "WorkEmail", "Department", "PictureURL", "AccountName"];

    for (var i = 0; i < users.length; i++) {
        var userProfilePropertiesForUser = new SP.UserProfiles.UserProfilePropertiesForUser(clientContext, users[i], profilePropertyNames);
        userProfileProperties[i] = peopleManager.getUserProfilePropertiesFor(userProfilePropertiesForUser);
    }

    clientContext.executeQueryAsync(onSuccess, onQueryError);


}



function onSuccess() {                

    var divUserProfiles = document.getElementById('divUserProfiles');
    var html = "<style type='text/css'> .floatL {float:left;margin:10px;} .floatR {padding-top:10px} .profile {padding:10px 10px;} .editProfile{margin-left:100px;}  div>img {height:72px;width:72px;} </style>";
    for (var i = 0; i < userProfileProperties.length; i++) {
        html += "<div class='profile'><div class='floatL'><img src='" + userProfileProperties[i][3] + "' href='#' /></div><div class='floatR'><h2><span><a href='' >" + userProfileProperties[i][0] + "</a></span></h2><span>Work Email : " + userProfileProperties[i][1] + "</span><br /><span>Department  : " + userProfileProperties[i][2] + "</span><br /></div></div><br />";
    }        

    divUserProfiles.innerHTML = html;

}

})(jQuery);

Solution

  • The following blog post shows how to use pagination with SharePoint Search and returning lists for pagination in general: https://www.microsofttrends.com/2015/08/03/paging-through-sharepoint-2013-office-365-lists-with-javascript/

    I imagine implementing pagination will probably resolve slow page load.