我正在用javascript为sharepoint执行循环。我希望它同步执行,这意味着只有在第一次数据执行完成后,它才应该从第二次开始。如果第一次数据执行完成(无论成功还是失败),那么在每个循环中只有第二次才应该开始。
$.each(selectedSiteCollections, function (index, value) {
act.SharePoint.SharePointAppProgress.addStatusRow(act.Resources.StatusInfo.applyFolderNavigation, "applyFolderNavigationId");
var siteUrl = value;//_spPageContextInfo.siteServerRelativeUrl + "/" + projectUrl;
var clientContext = act.SharePoint.SharePointAppCommon.getClientContext(siteUrl);
var newWebsite = clientContext.get_web();
var pageUrl = siteUrl + "/Shared%20Documents/Forms/AllItems.aspx";
var homePageUrl = siteUrl + "/Pages/Home.aspx";
var jsLinkUrl = '~siteCollection/_catalogs/masterpage/FolderNavigation.js'; //<-- specify JSLink url
var page = newWebsite.getFileByServerRelativeUrl(pageUrl);
var homePage = newWebsite.getFileByServerRelativeUrl(homePageUrl);
var wpm = page.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
var collWebPart = wpm.get_webParts();
var wpmHome = homePage.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
var collWebPartHome = wpmHome.get_webParts();
clientContext.load(homePage);
clientContext.load(collWebPart, 'Include(WebPart.Properties)');
clientContext.load(collWebPartHome, 'Include(WebPart.Properties)');
clientContext.executeQueryAsync(function () {
if (homePage.get_checkOutType() != SP.CheckOutType.online) {
homePage.checkOut();
}
var webPartDef = null;
for (var x = 0; x < collWebPart.get_count() && !webPartDef; x++) {
var temp = collWebPart.get_item(x);
webPartDef = temp;
}
if (webPartDef != null) {
var webpart = webPartDef.get_webPart();
var properties = webpart.get_properties();
properties.set_item('JSLink', jsLinkUrl);
webPartDef.saveWebPartChanges();
}
var webPartDefHome = null;
for (var x = 0; x < collWebPartHome.get_count() && !webPartDefHome; x++) {
var temp = collWebPartHome.get_item(x);
webPartDefHome = temp;
}
if (webPartDefHome != null) {
var webpart = webPartDefHome.get_webPart();
var properties = webpart.get_properties();
properties.set_item('JSLink', jsLinkUrl);
webPartDefHome.saveWebPartChanges();
}
homePage.checkIn("ACT Update webpart", SP.CheckinType.majorCheckIn);
homePage.publish("ACT Update webpart");
clientContext.executeQueryAsync(function () {
act.SharePoint.SharePointAppProgress.updateStatusRow(true, "applyFolderNavigationId");
}, function (sender, args) { executeAsyncFailed(sender, args, "applyFolderNavigationId"); });
}, function (sender, args) { executeAsyncFailed(sender, args, "applyFolderNavigationId"); });
});
您将无法运行包含异步调用(executeQueryAsync()
)的同步foreach循环。任何依赖于异步调用完成的逻辑都需要由异步回调函数触发,这些函数指示函数何时完成执行(以及它是失败还是成功)。
一个可行的方法是创建一个处理单个站点的函数。使用更高范围的变量来跟踪网站集合以及该集合中的当前索引。在executeQueryAsync()
调用的onSuccess和onFailure回调中,增加索引并调用函数来处理集合中的下一个站点。
我使用自定义循环逻辑做到了这一点:)
var loop = function (i) {
enableFolderNavigation(selectedSiteCollections[i], i, function () {
if (++i < selectedSiteCollections.length) {
loop(i);
} else {
act.SharePoint.SharePointAppProgress.completed(true, "Completed");
}
});
};
loop(0);
以及包含异步调用的函数
enableFolderNavigation = function (item, index, successFolderNavigation) {
var siteUrl = item.link;//_spPageContextInfo.siteServerRelativeUrl + "/" + projectUrl;
var title = item.title;
act.SharePoint.SharePointAppProgress.addStatusRow(act.Resources.StatusInfo.applyFolderNavigation + " :" + title, "applyFolderNavigationId" + index);
var clientContext = act.SharePoint.SharePointAppCommon.getClientContext(siteUrl);
var newWebsite = clientContext.get_web();
var pageUrl = siteUrl + "/Shared%20Documents/Forms/AllItems.aspx";
var homePageUrl = siteUrl + "/Pages/Home.aspx";
var jsLinkUrl = '~siteCollection/_catalogs/masterpage/FolderNavigation.js'; //<-- specify JSLink url
var page = newWebsite.getFileByServerRelativeUrl(pageUrl);
var homePage = newWebsite.getFileByServerRelativeUrl(homePageUrl);
var wpm = page.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
var collWebPart = wpm.get_webParts();
var wpmHome = homePage.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
var collWebPartHome = wpmHome.get_webParts();
clientContext.load(homePage);
clientContext.load(collWebPart, 'Include(WebPart.Properties)');
clientContext.load(collWebPartHome, 'Include(WebPart.Properties)');
clientContext.executeQueryAsync(function () {
// if (homePage.get_checkOutType() != SP.CheckOutType.online) {
homePage.checkOut();
// }
var webPartDef = null;
for (var x = 0; x < collWebPart.get_count() && !webPartDef; x++) {
var temp = collWebPart.get_item(x);
webPartDef = temp;
}
if (webPartDef != null) {
var webpart = webPartDef.get_webPart();
var properties = webpart.get_properties();
properties.set_item('JSLink', jsLinkUrl);
webPartDef.saveWebPartChanges();
}
var webPartDefHome = null;
for (var x = 0; x < collWebPartHome.get_count() && !webPartDefHome; x++) {
var temp = collWebPartHome.get_item(x);
webPartDefHome = temp;
}
if (webPartDefHome != null) {
var webpart = webPartDefHome.get_webPart();
var properties = webpart.get_properties();
properties.set_item('JSLink', jsLinkUrl);
webPartDefHome.saveWebPartChanges();
}
homePage.checkIn("ACT Update webpart", SP.CheckinType.majorCheckIn);
homePage.publish("ACT Update webpart");
clientContext.executeQueryAsync(function () {
act.SharePoint.SharePointAppProgress.updateStatusRow(true, "applyFolderNavigationId" + index);
successFolderNavigation();
}, function (sender, args) { executeAsyncFailed(sender, args, "applyFolderNavigationId" + index); successFolderNavigation(); });
}, function (sender, args) { executeAsyncFailed(sender, args, "applyFolderNavigationId" + index); successFolderNavigation(); });
},