A field in a SharePoint list form like NewForm.aspx or EditForm.aspx can be easily hidden by using the following Jquery statements. Assume that we have an input field named Feedback

var feedbackField = $("input[title=Feedback]");
feedbackField.parent().parent().parent().hide();   
But what if we want to hide the field for a particular SharePoint user group only. Let's say we have a user group named "Test Group" and we don't want to show the Feedback field to the users of this group. In order to achieve this we need to know whether the current logged in user belongs to the "Test Group" or not.This can be done by using  SPServices, a Jquery library for SharePoint web services.
Grab the latest version of SPServices Jquery library from codeplex and Jquery from here.
Once you have both .js files, upload them to a SharePoint document library. In my case I created a folder named JS inside Shared Documents Document library and uploaded both files inside it. Next Create any SharePoint list and create a Text column named Feedback in it. Now we will add a content editor webpart to the NewForm.aspx page of this list. In order to edit the NewForm.aspx page append 
?PageView=Shared&ToolPaneView=2 to the NewForm.aspx page in the url. Your url should look something like this http://ws2003/Lists/Project%20Tasks/NewForm.aspx?PageView=Shared&ToolPaneView=2. Now add a content editor webpart to the page and the following script to the content editor webpart.

<script type="text/javascript" src="/Shared%20Documents/JS/jquery.min.js"></script>
<script type="text/javascript" language="javascript" src="/Shared%20Documents/JS/jquery.SPServices-0.6.2.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
  $().SPServices({
    operation: "GetGroupCollectionFromUser",
    userLoginName: $().SPServices.SPGetCurrentUser(),
    async: false,
       completefunc: function (xData, Status) {
       var xml = xData.responseXML.xml;
       if (xml.search('Test Group') != -1)
  {
         var feedbackField = $("input[title=Feedback]");
         feedbackField.parent().parent().parent().hide();       
  }
     }
  });
});
</script>

The SPServices method GetGroupCollectionFromUser gets all the groups of a particular user and $().SPServices.SPGetCurrentUser() supplies the current user as input parameter to the GetGroupCollectionFromUser. A user may belong to several groups, so we need to find whether the user belongs to the group we are interested in (Test Group in our case). In the original code sample present here , this is done by using Jquery find method if($(xData.responseXML).find("Group[Name='GroupName']").length == 1) but this did not seem to work for me. So I had to use javascript search method. Same script can be added in EditFrom.aspx page as well.


2 comments

  1. Cameron McGill // 22 January 2014 05:25  

    Would you mind posting the full code for the top snippet only? I'm not a coder, but I'd like to use the code in my CEWP, but I don't know how to enclose this to make it runnable.

    var feedbackField = $("input[title=Feedback]");
    feedbackField.parent().parent().parent().hide();

  2. Nadeem Yousuf // 22 January 2014 05:36  

    Hi Cameron,

    You need to first download JQuery and then add in a Document Library in your site. You need to change the path of JQuery based on where you put it in the below code:





    $(document).ready(function() {

    var feedbackField = $("input[title=Feedback]");

    feedbackField.parent().parent().parent().hide();

    });

My SharePoint CodePlex Projects

SharePoint 2010 Autocomplete Lookup Field

Export Version History Of SharePoint 2010 List Items to Microsoft Excel.