{"id":1743,"date":"2016-12-22T16:29:53","date_gmt":"2016-12-22T15:29:53","guid":{"rendered":"https:\/\/blog.kodono.info\/wordpress\/?p=1743"},"modified":"2016-12-22T17:59:57","modified_gmt":"2016-12-22T16:59:57","slug":"2-bypass-a-lookup-field-not-displayed-because-of-threshold-on-newform-and-editform-sharepoint-2013","status":"publish","type":"post","link":"https:\/\/blog.kodono.info\/wordpress\/2016\/12\/22\/2-bypass-a-lookup-field-not-displayed-because-of-threshold-on-newform-and-editform-sharepoint-2013\/","title":{"rendered":"[2] Bypass a lookup field not displayed because of threshold on NewForm and EditForm [Sharepoint 2013]"},"content":{"rendered":"<p>This is an update of <a href=\"https:\/\/blog.kodono.info\/wordpress\/2016\/08\/25\/bypass-a-lookup-field-not-displayed-because-of-threshold-on-newform-and-editform-sharepoint-2013\/\">this article<\/a>.<\/p>\n<p>This time the JS Link file&#8217;s content is the one below:<\/p>\n<pre class=\"brush:javascript\">\r\n\/\/ loadExt permits to load JS and CSS files\r\n\/\/ https:\/\/gist.github.com\/Aymkdn\/98acfbb46fbe7c1f00cdd3c753520ea8\r\nfunction 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&lt;s.js.length?s.loadScript(e):s.after()};t.onload=function(){a()},s.head.appendChild(t)};for(var a=0;a&lt;s.files.length;a++)\/\\.js$|\\.js\\?\/.test(s.files[a])&#038;&s.js.push(s.files[a]),\/\\.css$|\\.css\\?\/.test(s.files[a])&#038;&s.loadStyle(s.files[a]);s.js.length>0?s.loadScript(0):s.after()}\r\n\r\n(function() {\r\n  \/\/ some \"global\" variables\r\n  var lookupFieldsToFix = [];\r\n  var storageThrottledLookup = localStorage.getItem(\"ThrottledLookup\");\r\n  if (storageThrottledLookup) storageThrottledLookup=JSON.parse(storageThrottledLookup);\r\n\r\n  \/\/ onLoad() is called when all the fields in the form have been proceed\r\n  function onLoad(ctx) {\r\n    \/\/ we'll need jQuery (or nanoajax... see SharepointPlus doc) and SharepointPlus\r\n    loadExt([\r\n      \"\/Toolbox\/Documents\/js\/jQuery\/jquery-1.12.4.min.js\",\r\n      \"\/Toolbox\/Documents\/js\/SharepointPlus\/3.14\/sharepointplus-3.14.js\"\r\n    ], function() {\r\n      \/\/ if some lookup fields need to be fixed\r\n      if (lookupFieldsToFix.length>0) {\r\n        var aDeferred=[];\r\n        for (var i=0; i&lt;lookupFieldsToFix.length; i++) {\r\n          aDeferred.push(fixLookup(lookupFieldsToFix[i]));\r\n        }\r\n        $.when.apply(this, aDeferred).done(function() {\r\n          var save={}, count=0, i;\r\n          for (i=arguments.length; i--;) {\r\n            if (arguments[i]) {\r\n              save[arguments[i].field] = arguments[i].choices; \/\/ save it as {fieldName:choices}\r\n              count++;\r\n            }\r\n          }\r\n          if (count > 0) {\r\n            \/\/ we use localStorage to store the data retrieve from the Lists\r\n            localStorage.setItem('ThrottledLookup', JSON.stringify(save));\r\n            \/\/ now reload the page\r\n            $SP().closeModalDialog();\r\n            $('#aspnetForm').hide().after('&lt;h1 style=\"margin-top:50vh;transform:translateY(-50%);text-align:center\">Reloading...&lt;\/h1>');\r\n            window.location.reload();\r\n          }\r\n        })\r\n      } else {\r\n        \/\/ delete all existing localStorage\r\n        localStorage.removeItem('ThrottledLookup');\r\n\r\n        \/\/ here you can start doing anything you want on this form\r\n      }\r\n    });    \r\n  }\r\n  \r\n  \/**\r\n   * Fix the broken lookup fields\r\n   * @param  {String} field Name of the field to fix\r\n   * @return {Deferred}\r\n   *\/ \r\n  function fixLookup(field) {\r\n    var deferred = jQuery.Deferred();\r\n    var choices=[], modal;\r\n\r\n    WPQ2FormCtx.ListSchema[field].ChoiceCount=0;\r\n    WPQ2FormCtx.ListSchema[field].Choices=[];\r\n    \/\/ show a Waiting message\r\n    modal = $SP().getModalDialog('loading-throttled');\r\n    if (!modal) {\r\n      $SP().showModalDialog({\r\n        wait:true,\r\n        title:\"Loading some data...\",\r\n        id:\"loading-throttled\"\r\n      });\r\n    }\r\n    \/\/ and we get data from the list\r\n    \/\/ here we'll use \"ID\" as the data returned by our field in the form\r\n    var fieldID=\"ID\";\r\n    $SP().list('{'+WPQ2FormCtx.ListSchema[field].LookupListId+'}').get({fields:fieldID, paging:true}, function(data) {\r\n      var res=[];\r\n      for (var i=0, len=data.length; i&lt;len; i++) {\r\n        res.push({LookupId:data[i].getAttribute(fieldID), LookupValue:data[i].getAttribute(fieldID)});\r\n      }\r\n      deferred.resolve({field:field, choices:res});\r\n    });\r\n\r\n    return deferred;\r\n  }\r\n\r\n  \/\/ do some actions as soon as the fields are shown\r\n  var loadAfterForm = {\r\n    Templates: {\r\n      OnPreRender:function(ctx) {\r\n        \/\/ we want to show Lookup fields even if there are more than 5000 items in those lists\r\n        if (ctx.ListSchema.Field[0].Throttled === true) {\r\n          if (storageThrottledLookup) {\r\n            \/\/ we use the stored data to create our dropdown\r\n            ctx.ListSchema.Field[0].Throttled=false;\r\n            ctx.ListSchema.Field[0].ChoiceCount=storageThrottledLookup[ctx.ListSchema.Field[0].Name].length;\r\n            ctx.ListSchema.Field[0].Choices=storageThrottledLookup[ctx.ListSchema.Field[0].Name];\r\n          }\r\n          else {\r\n            lookupFieldsToFix.push(ctx.ListSchema.Field[0].Name);\r\n          }\r\n        }\r\n      },\r\n      OnPostRender:function(ctx) {\r\n        \/\/ only trigger when everything is loaded\r\n        \/\/ --> ctx.ListData.Items[0] all the fields\r\n        if (ctx.ListSchema.Field[0].Name === \"Attachments\") {\r\n          onLoad(ctx)\r\n        }\r\n      }\r\n    }\r\n  }\r\n\r\n  \/\/ don't do it when editing the page\r\n  if (GetUrlKeyValue(\"PageView\") !== \"Shared\" &#038;& GetUrlKeyValue(\"DisplayMode\") !== \"Design\") SPClientTemplates.TemplateManager.RegisterTemplateOverrides(loadAfterForm);\r\n})();\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is an update of this article. This time the JS Link file&#8217;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(&#8220;head&#8221;)[0],s.after=t||function(){},s.loadStyle=function(e){var t=document.createElement(&#8220;link&#8221;);t.rel=&#8221;stylesheet&#8221;,t.type=&#8221;text\/css&#8221;,t.href=e,s.head.appendChild(t)},s.loadScript=function(e){var t=document.createElement(&#8220;script&#8221;);t.type=&#8221;text\/javascript&#8221;,t.src=s.js[e];var a=function(){++e&lt;s.js.length?s.loadScript(e):s.after()};t.onload=function(){a()},s.head.appendChild(t)};for(var a=0;a&lt;s.files.length;a++)\/\\.js$|\\.js\\?\/.test(s.files[a])&#038;&#038;s.js.push(s.files[a]),\/\\.css$|\\.css\\?\/.test(s.files[a])&#038;&#038;s.loadStyle(s.files[a]);s.js.length>0?s.loadScript(0):s.after()} (function() { \/\/ some &#8220;global&#8221; variables var lookupFieldsToFix = []; var storageThrottledLookup = localStorage.getItem(&#8220;ThrottledLookup&#8221;); if (storageThrottledLookup) storageThrottledLookup=JSON.parse(storageThrottledLookup); \/\/ onLoad() [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","hide_page_title":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-1743","post","type-post","status-publish","format-standard","hentry","category-divers"],"_links":{"self":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1743","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/comments?post=1743"}],"version-history":[{"count":7,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1743\/revisions"}],"predecessor-version":[{"id":1751,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1743\/revisions\/1751"}],"wp:attachment":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/media?parent=1743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/categories?post=1743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/tags?post=1743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}