Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Sharepoint Online Add-In JSOM Fetch All Users too long

$
0
0

Hello!

I created app in sharepoint online where I get all userswith 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 workscorrectly, butthe resultpage (350 users) load timeis very long (~15 seconds).

Apparently, the mainwait time-this issending POSTrequestandreceiving a response (~10 seconds).

How can Ioptimizethis 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);



Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>