JSLink is one of the most widely used CSR (client-side rendering) techniques used by SharePoint developers. In today’s article, we will learn how to use JS Link to show or hide the fields based on the logged in user group.

We had got one of the requirements from our client to hide/show fields in the new form as per the user logged in the group.

Columns description

The below table shows the fields that should be shown/hidden according to the logged in user group

SL NoUser GroupFields to be shownFields to be Hidden
1.Client IndiaTitle,ClientID,ClientNameDeveloperID,DeveloperName, ArchitectID, ArchitectName
2.Developers IndiaTitle,DeveloperID,DeveloperNameClientID,ClientName,ArchitectID, ArchitectName
3.Architects IndiaTitle, ArchitectID, ArchitectNameDeveloperID,DeveloperName, ClientID,ClientName

Below is the JSLink code sample which we used to hide/show fields based on the user group

  1. (function () {
  2. // Create object that have the context information about the field that we want to change it's output render
  3. var hidecontext = {};
  4. var groups=["Architects India","Client India","Developers India"];
  5. var checkarchitect=null;
  6. var checkdeveloper=null;
  7. var checkclient=null;
  8. var rendercount=0;
  9. hidecontext.Templates = {};
  10. hidecontext.OnPreRender=function(ctx){
  11. for(var i=0; i<groups.length;i++)
  12. {
  13. if(groups[i].toString()=="Architects India")
  14. {
  15. $.ajax({
  16. url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/sitegroups/getByName('"+ groups[i] +"')/Users?$filter=Id eq "+ _spPageContextInfo.userId,
  17. method: "GET",
  18. headers: { "Accept": "application/json; odata=verbose" },
  19. success: function(data){
  20. if(data.d.results[0]!= undefined)
  21. {
  22. checkarchitect=true;
  23. console.log("checkarchitect value "+checkarchitect);
  24. console.log(JSON.stringify(data.d.results));
  25. console.log("Current user is part of the architects india group");
  26. //hidefields();
  27. }
  28. }
  29. });
  30. }
  31. else if(groups[i].toString()=="Client India")
  32. {
  33. $.ajax({
  34. url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/sitegroups/getByName('"+ groups[i] +"')/Users?$filter=Id eq "+ _spPageContextInfo.userId,
  35. method: "GET",
  36. headers: { "Accept": "application/json; odata=verbose" },
  37. success: function(data){
  38. if(data.d.results[0] != undefined){
  39. checkclient=true;
  40. console.log("checkclient value "+checkclient);
  41. console.log("Current user is part of the Client India group");
  42. //hidefields();
  43. }
  44. }
  45. });
  46. }
  47. else if(groups[i].toString()=="Developers India")
  48. {
  49. $.ajax({
  50. url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/sitegroups/getByName('"+ groups[i] +"')/Users?$filter=Id eq "+ _spPageContextInfo.userId,
  51. method: "GET",
  52. headers: { "Accept": "application/json; odata=verbose" },
  53. success: function(data){
  54. if(data.d.results[0] != undefined)
  55. {
  56. checkdeveloper=true;
  57. console.log("checkdeveloper value "+checkdeveloper);
  58. console.log("Current user is part of the Developers India group");
  59. //hidefields();
  60. }
  61. }
  62. });
  63. }
  64. }
  65. };
  66. hidecontext.OnPostRender=function(Ctx){
  67. hidefields();
  68. console.log("post rendering fired"+checkarchitect+"value of architect"+checkclient+"value of thecheckclient"+checkdeveloper+"value of checkdeveloper");
  69. if(rendercount==0)
  70. {
  71. $("<div id='UserTitle' style='text-align:center;'>Appropriate Users Click to show the respective Fields</div>").insertBefore(($("input[title='Title Required Field']")).parents("table:first"));
  72. rendercount++;
  73. }
  74. $("#UserTitle").click(function(){
  75. console.log("UserTitle div click event fired");
  76. if(checkarchitect!=null && checkarchitect==true)
  77. {
  78. console.log("architect section should be shown now");
  79. $("input[title='ClientID']").closest("tr").hide();
  80. $("input[title='ClientName']").closest("tr").hide();
  81. $("input[title='DeveloperName']").closest("tr").hide();
  82. $("input[title='DeveloperID']").closest("tr").hide();
  83. $("input[title='Title Required Field']").closest("tr").show();
  84. $("input[title='ArchitectName']").closest("tr").show();
  85. $("input[title='ArchitectID']").closest("tr").show();
  86. }
  87. else if(checkclient!=null && checkclient==true)
  88. {
  89. console.log("client section should be shown now");
  90. $("input[title='ArchitectName']").closest("tr").hide();
  91. $("input[title='ArchitectID']").closest("tr").hide();
  92. $("input[title='DeveloperName']").closest("tr").hide();
  93. $("input[title='DeveloperID']").closest("tr").hide();
  94. $("input[title='ClientID']").closest("tr").show();
  95. $("input[title='ClientName']").closest("tr").show();
  96. $("input[title='Title Required Field']").closest("tr").show();
  97. }
  98. else if(checkdeveloper!=null && checkdeveloper==true)
  99. {
  100. console.log("developer section should be shown now");
  101. $("input[title='ArchitectName']").closest("tr").hide();
  102. $("input[title='ArchitectID']").closest("tr").hide();
  103. $("input[title='ClientID']").closest("tr").hide();
  104. $("input[title='ClientName']").closest("tr").hide();
  105. $("input[title='DeveloperName']").closest("tr").show();
  106. $("input[title='DeveloperID']").closest("tr").show();
  107. $("input[title='Title Required Field']").closest("tr").show();
  108. }
  109. else
  110. {
  111. console.log("status not showing up");
  112. }
  113. });
  114. };
  115. SPClientTemplates.TemplateManager.RegisterTemplateOverrides(hidecontext);
  116. function hidefields()
  117. {
  118. $("input[title='ArchitectName']").closest("tr").hide();
  119. $("input[title='ArchitectID']").closest("tr").hide();
  120. $("input[title='ClientID']").closest("tr").hide();
  121. $("input[title='ClientName']").closest("tr").hide();
  122. $("input[title='DeveloperName']").closest("tr").hide();
  123. $("input[title='DeveloperID']").closest("tr").hide();
  124. $("input[title='Title Required Field']").closest("tr").hide();
  125. }
  126. })();

Explanation of the code sample: