Hide a SharePoint list field based on user group using JQuery Webservice in 2010

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();       
}
     }
  });
});

Comments

  1. Acting deals with very delicate emotions. It is not putting up a mask. Each time an actor acts he does not hide; he exposes himself. See the link below for more info.


    #hide
    www.ufgop.org

    ReplyDelete
  2. This is very helpful. But I failed to perform the function. Could you explain more details of how to open and edit the NewForm.aspx page? Using a Notepad or edit it in IE/Firefox ? Thanks!

    ReplyDelete
  3. Dear Ravi, great article but What if you have multiple groups.

    ReplyDelete

Post a Comment

Popular posts from this blog

SharePoint 2016 and 2019 Ports

PsConfig step by step /Configuration Wizard. “Upgrade Available” vs “Upgrade required”

Unlock the SharePoint site using Powershell command