[2] Bypass a lookup field not displayed because of threshold on NewForm and EditForm [Sharepoint 2013]

This is an update of this article.

This time the JS Link file’s content is the one below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// loadExt permits to load JS and CSS files
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);
})();

Leave a Reply

Your email address will not be published. Required fields are marked *

*