This is an update of this article.
This time the JS Link file’s content is the one below:
// loadExt permits to load JS and CSS files
// https://gist.github.com/Aymkdn/98acfbb46fbe7c1f00cdd3c753520ea8
function loadExt(e,t){var s=this;s.files=e,s.js=[],s.head=document.getElementsByTagName("head")[0],s.after=t||function(){},s.loadStyle=function(e){var t=document.createElement("link");t.rel="stylesheet",t.type="text/css",t.href=e,s.head.appendChild(t)},s.loadScript=function(e){var t=document.createElement("script");t.type="text/javascript",t.src=s.js[e];var a=function(){++e<s.js.length?s.loadScript(e):s.after()};t.onload=function(){a()},s.head.appendChild(t)};for(var a=0;a<s.files.length;a++)/\.js$|\.js\?/.test(s.files[a])&&s.js.push(s.files[a]),/\.css$|\.css\?/.test(s.files[a])&&s.loadStyle(s.files[a]);s.js.length>0?s.loadScript(0):s.after()}
(function() {
// some "global" variables
var lookupFieldsToFix = [];
var storageThrottledLookup = localStorage.getItem("ThrottledLookup");
if (storageThrottledLookup) storageThrottledLookup=JSON.parse(storageThrottledLookup);
// onLoad() is called when all the fields in the form have been proceed
function onLoad(ctx) {
// we'll need jQuery (or nanoajax... see SharepointPlus doc) and SharepointPlus
loadExt([
"/Toolbox/Documents/js/jQuery/jquery-1.12.4.min.js",
"/Toolbox/Documents/js/SharepointPlus/3.14/sharepointplus-3.14.js"
], function() {
// if some lookup fields need to be fixed
if (lookupFieldsToFix.length>0) {
var aDeferred=[];
for (var i=0; i<lookupFieldsToFix.length; i++) {
aDeferred.push(fixLookup(lookupFieldsToFix[i]));
}
$.when.apply(this, aDeferred).done(function() {
var save={}, count=0, i;
for (i=arguments.length; i--;) {
if (arguments[i]) {
save[arguments[i].field] = arguments[i].choices; // save it as {fieldName:choices}
count++;
}
}
if (count > 0) {
// we use localStorage to store the data retrieve from the Lists
localStorage.setItem('ThrottledLookup', JSON.stringify(save));
// now reload the page
$SP().closeModalDialog();
$('#aspnetForm').hide().after('<h1 style="margin-top:50vh;transform:translateY(-50%);text-align:center">Reloading...</h1>');
window.location.reload();
}
})
} else {
// delete all existing localStorage
localStorage.removeItem('ThrottledLookup');
// here you can start doing anything you want on this form
}
});
}
/**
* Fix the broken lookup fields
* @param {String} field Name of the field to fix
* @return {Deferred}
*/
function fixLookup(field) {
var deferred = jQuery.Deferred();
var choices=[], modal;
WPQ2FormCtx.ListSchema[field].ChoiceCount=0;
WPQ2FormCtx.ListSchema[field].Choices=[];
// show a Waiting message
modal = $SP().getModalDialog('loading-throttled');
if (!modal) {
$SP().showModalDialog({
wait:true,
title:"Loading some data...",
id:"loading-throttled"
});
}
// and we get data from the list
// here we'll use "ID" as the data returned by our field in the form
var fieldID="ID";
$SP().list('{'+WPQ2FormCtx.ListSchema[field].LookupListId+'}').get({fields:fieldID, paging:true}, function(data) {
var res=[];
for (var i=0, len=data.length; i<len; i++) {
res.push({LookupId:data[i].getAttribute(fieldID), LookupValue:data[i].getAttribute(fieldID)});
}
deferred.resolve({field:field, choices:res});
});
return deferred;
}
// do some actions as soon as the fields are shown
var loadAfterForm = {
Templates: {
OnPreRender:function(ctx) {
// we want to show Lookup fields even if there are more than 5000 items in those lists
if (ctx.ListSchema.Field[0].Throttled === true) {
if (storageThrottledLookup) {
// we use the stored data to create our dropdown
ctx.ListSchema.Field[0].Throttled=false;
ctx.ListSchema.Field[0].ChoiceCount=storageThrottledLookup[ctx.ListSchema.Field[0].Name].length;
ctx.ListSchema.Field[0].Choices=storageThrottledLookup[ctx.ListSchema.Field[0].Name];
}
else {
lookupFieldsToFix.push(ctx.ListSchema.Field[0].Name);
}
}
},
OnPostRender:function(ctx) {
// only trigger when everything is loaded
// --> ctx.ListData.Items[0] all the fields
if (ctx.ListSchema.Field[0].Name === "Attachments") {
onLoad(ctx)
}
}
}
}
// don't do it when editing the page
if (GetUrlKeyValue("PageView") !== "Shared" && GetUrlKeyValue("DisplayMode") !== "Design") SPClientTemplates.TemplateManager.RegisterTemplateOverrides(loadAfterForm);
})();