Sharepoint Online Add-In JSOM获取所有用户太长时间

时间:2016-04-12 12:41:49

标签: sharepoint-2013 sharepoint-online sharepoint-userprofile sharepoint-jsom

我在SharePoint中创建了应用程序,我向所有用户提供了他们的属性列表(http://social.technet.microsoft.com/wiki/contents/articles/25074.sharepoint-online-working-with-people-search-and-user-profiles.aspx)。

应用程序正常运行,但结果页面(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);

1 个答案:

答案 0 :(得分:0)

以下博文显示如何使用SharePoint搜索分页并返回一般分页列表:https://www.microsofttrends.com/2015/08/03/paging-through-sharepoint-2013-office-365-lists-with-javascript/

我想实现分页可能会解决页面加载缓慢的问题。