0

我在SharePoint上創建了應用程序,在那裏我得到所有用戶的屬性列表(http://social.technet.microsoft.com/wiki/contents/articles/25074.sharepoint-online-working-with-people-search-and-user-profiles.aspx)。Sharepoint在線加載項JSOM獲取所有用戶太長

該應用程序工作正常,但結果頁面(350個用戶)加載時間很長(〜15秒)。

顯然,主要的等待時間 - 這是發送POST請求和接收響應(~10秒)。

如何優化此應用程序?

'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); 

回答

相關問題