Hide a field into a form with JSLink based on field’s description [Sharepoint]

On Sharepoint 2013 I was trying to hide a field (the complete row) based on some elements into its description.

To do so we will call our file using the JSLink (see my previous post about it).
The JS file looks like that:

(function() {
  // do some actions as soon as the fields are shown
  var loadAfterForm = {
    Templates: {
      OnPostRender:function(ctx) {
        // hide the field/row when "[INTERNAL USE - DO NOT CHANGE]" is detected in the field's description
        if (ctx.ListSchema.Field[0].Description.indexOf('[INTERNAL USE - DO NOT CHANGE]') > -1) {
          // get the element that represents the field
          var elem = document.querySelector('[id^="'+ctx.ListSchema.Field[0].Name +'_'+ ctx.ListSchema.Field[0].Id+'"]');
          while (elem) {
            elem = elem.parentNode;
            if (elem) {
              if (elem.tagName==="BODY") break;
              if (elem.tagName === "TD" && elem.className.indexOf("ms-formbody") > -1) {
                // hide the whole row
                elem.parentNode.style.display="none";
                break;
              }
            }
          }
        }
      }
    }
  }
  SPClientTemplates.TemplateManager.RegisterTemplateOverrides(loadAfterForm);
})();

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

*