Monday, July 15, 2013

Hide SharePoint List Columns based on User Permissions

Requirement: We've a "Requests" link in our Help Desk site. The "Status" field in help desk requests list should be hidden when users create new entry in the list. Same time, Status field must be visible to people in "Help desk operators" group.

So, the requirement is to hide SharePoint List Form field based on user permissions!

Solution: Use SPServices to check whether the current user is member of a particular group. If not,  hide the field using jQuery (or you can make the field Read-only too: How to Make SharePoint List Column Read Only ). Here is the detailed steps:

  1. Place the below script in a text file, upload to any SharePoint library of the site.
  2. Edit the NewForm.aspx, Add a content editor web part just below form fields, point the script file in CEWP and make the content editor web part hidden.
<!-- jQuery Reference. You can refer it from Layouts Folder/Doc Library too, after uploading the script. -->
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
 
<!-- Download SPServices from: http://spservices.codeplex.com/ Or use this CDN  -->
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/0.7.1a/jquery.SPServices-0.7.1a.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {
 $().SPServices({
  operation: "GetGroupCollectionFromUser",
  userLoginName: $().SPServices.SPGetCurrentUser(),
  async: false,
  completefunc: function(xData, Status) {
  var xml = xData.responseXML.xml;

   //If the current User does belong to the group "Service desk Operators"
    if (xml.search('Service Desk Operators') == -1)
     {
          //   alert("No, User Doesn't Exists!");                   
           $("select[title=Status]").parent().parent().parent().hide();  
          //or use: $('td.ms-formlabel:contains("Status")').parent().hide();
          //You can also use:  $('nobr:contains("Status")').closest('tr').hide();
       }   
    }
 });
});
</script>

Instead of content editor web part, You can also edit the NewForm.aspx or EditForm.aspx file and place the code under "PlaceHolderAdditionalPageHead" to hide fields in SharePoint list forms.

Here is the NewForm.aspx view for "Help Desk Operators" - Note that the "Status" field is visible.
hide sharepoint list columns based on user permissions
 and here is the view for end-users: SharePoint list columns hidden based user permissions.
hide sharepoint list form fields columns based on user permissions



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

12 comments :

  1. Hi,

    Running the above code gets this error: Uncaught TypeError: Cannot call method 'search' of undefined

    Any ideas why? The error is on this line of code: if (xml.search('PUBLISHERS') == -1)

    SPServices.js pulled form a Doc Lib in my site collection.

    *PUBLISHERS is the name of my group*

    Full code (All ">" pulled out of the Script tags to pass the comment validation:
    script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js">/script
    scipt type="text/javascript" src="../../SiteAssets/Scripts/jquery_SPServices-2013_01.min.js">/sript

    script type="text/javascript"
    $(document).ready(function() {
    $().SPServices({
    operation: "GetGroupCollectionFromUser",
    userLoginName: $().SPServices.SPGetCurrentUser(),
    async: false,
    completefunc: function(xData, Status) {
    var xml = xData.responseXML.xml;

    //If the current User does belong to the group "Service desk Operators"
    if (xml.search('PUBLISHERS') == -1)
    {
    // alert("No, User Doesn't Exists!");
    $("select[title=ViewBy]").parent().parent().parent().hide();
    //or use: $('td.ms-formlabel:contains("Status")').parent().hide();
    //You can also use: $('nobr:contains("Status")').closest('tr').hide();
    }
    }
    });
    });
    /script

    thanks,

    Jon

    ReplyDelete
    Replies
    1. Jon,

      Just validate the file paths of the referenced js files ( SPServices.js & jQuery.js). Not sure that the new versions of SPServices undergone changes. Try using the same version as in my code.

      Delete
    2. Hi
      Thanks for providing the script.
      The IF command was not working for me as well. Jon, have you resolved this error?
      Salaudeen, could you please help me on this.

      Delete
  2. Hi Paul, Jquey works only when user is directly added to the SharePoint group. It doesn't work when user is added to AD security group and this security group is added to SharePoint group.
    Did you ever encounter this issue?
    Regards,
    Khushi

    ReplyDelete
  3. Does this work with SharePoint 2013?

    ReplyDelete
  4. not working in chrome or mozilla

    ReplyDelete
  5. its not working in other browsers ,
    can you please provide the solution

    ReplyDelete
  6. Thanks for the workaround. However, My field doesn't hide even though I am not member of the group. I tried putting alert() on xData (coming as "object Object" error) and xml (coming as "undefined" error). I am referring to the correct path for both *.js files. Am I doing anything wrong?

    ReplyDelete
  7. Hello Salaudeen,
    Is there a way users can only see certain company, regardless of site permissions or list permissions? first i merged various lists in one list with same columns, for example: company 1 list, company 2 list, ....company 10 list etc. Let's say all those company list has common column called company number. Then i grouped according to company number.
    Now, can we let only certain users view/open certain groups?
    Please help.

    ReplyDelete
  8. Hi Salaudeen Rajack,
    Thank you for this script.
    It is very good and it is workin correctly on IE.

    I have two questions:
    1. Is there any chance to make fileld readonly instead of hidding it?
    2. Why this scrip does not work at all in other browsers?

    Thanks you once again!
    Great script!

    ReplyDelete
  9. Hi,
    Thanks for the jQuery.
    I am getting error at completefunc: function (xData, Status), could you please help me in resolving the issue.

    ReplyDelete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...