在共享点 javascript 模型的每个循环中同步执行 QueryAsync
executeQueryAsync synchronously in each loop for sharepoint javascript model
我正在 javascript 中为共享点执行循环。我希望它同步执行,这意味着只有在第一次数据执行完成后,它才应该从第二次开始。如果第一个数据执行完成(无论成功还是失败),那么在每个循环中只有它应该从第二个开始。
$.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"); });
});
您将无法 运行 包含异步调用的同步 foreach 循环 (executeQueryAsync()
)。任何依赖于异步调用完成的逻辑都需要由异步回调函数触发,异步回调函数指示函数何时完成执行(以及它是失败还是成功)。
一种可行的方法是创建一个处理单个 站点的函数。在更高范围内使用变量来跟踪站点集合和该集合中的当前索引。在 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(); });
},
我正在 javascript 中为共享点执行循环。我希望它同步执行,这意味着只有在第一次数据执行完成后,它才应该从第二次开始。如果第一个数据执行完成(无论成功还是失败),那么在每个循环中只有它应该从第二个开始。
$.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"); });
});
您将无法 运行 包含异步调用的同步 foreach 循环 (executeQueryAsync()
)。任何依赖于异步调用完成的逻辑都需要由异步回调函数触发,异步回调函数指示函数何时完成执行(以及它是失败还是成功)。
一种可行的方法是创建一个处理单个 站点的函数。在更高范围内使用变量来跟踪站点集合和该集合中的当前索引。在 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(); });
},