Sunday, October 7, 2018

Code to hide a column on SharePoint's OOB List's EditForm or NewForm to all users except users who are part of a specific SharePoint group.

Environment: SharePoint Online
Coded available In : JavaScript
Pre-requisite: 1) Jquery.js
                       2)jquery.SPServices-0.7.1a.min.js

Note: Make sure your SP Group should be visible for every one. This can be enabled in group settings.



<script type="text/javascript" src="https://sitecollectionORsiteurl/SiteAssets/jquery.SPServices-0.7.1a.min.js"></script>
&nbsp;
<script type="text/javascript">
var isValidatUser=false;

$(document).ready(function() {

 $().SPServices({
  operation: "GetGroupCollectionFromUser",
  userLoginName: $().SPServices.SPGetCurrentUser(),
  async: false,
  completefunc: function(xData, Status) {


if ($(xData.responseXML).find("Group[Name='Quality Owners']").length == 1) {
 //user in group

$("td nobr:contains('Column Display Name')").closest("tr").show();

}
else {
// user not part of group
$("td nobr:contains('Column Display Name')").closest("tr").hide();
}
       
    }
 });
});
</script>
In this article, you will see how to check logged in user is part of a given SharePoint Group. This is for SharePoint online environment and by using javascript.

you can put this code in content editor webpart to test.

Before using this script, make sure you have the file "jquery.SPServices-0.7.1a.min.js" in any library that you can refer in below code. In below example, i used the file under "SiteAssets\"

Also please make sure Jquery.js file is referred in your page already.




<script type="text/javascript" src="https://sharepointsitecollection/site1/SiteAssets/jquery.SPServices-0.7.1a.min.js"></script>

<script type="text/javascript">
var isValidatUser=false;

$(document).ready(function() {

 $().SPServices({
  operation: "GetGroupCollectionFromUser",
  userLoginName: $().SPServices.SPGetCurrentUser(),
  async: false,
  completefunc: function(xData, Status) {
 

if ($(xData.responseXML).find("Group[Name='SP Group Name']").length == 1)
{

//user in group

}
else {

// user not part of group


}
         
    }
 });
});
</script>