0

We have one list in SharePoint Online in which there are certain records. There is one column whose data should be visible to all users,but should not be edited by all the users, it should be enabled for editing only for a specific SharePoint group.

We don't want to use workflow/Lookup columns/Hide the column. Is there any way to achieve this with jQuery which maybe I can insert on EditForm.aspx ?

8
  • Are you on classic sharepoint site? Commented Feb 17, 2019 at 3:48
  • Yes. We have not moved to Modern yet.
    – Ravi
    Commented Feb 17, 2019 at 3:53
  • OK... Then you can check the current user permissions and if he not a part of particular group then you can hide the fields. Commented Feb 17, 2019 at 3:54
  • Or make them readonly. Commented Feb 17, 2019 at 3:55
  • Yes, We don't want to hide them So I am trying to make them readonly but can not figure out the way to do that.
    – Ravi
    Commented Feb 17, 2019 at 3:58

2 Answers 2

2

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).

<!-- 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.   
       if($(xData.responseXML).find("Group[Name='Vertical Owners']").length != 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>

Using REST API: You can also use the SharePoint REST API to achieve the same:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">

IsCurrentUserMemberOfGroup = function (groupName) {

  var result = false;
  $.ajax({
    url: _spPageContextInfo.webAbsoluteUrl +'/_api/web/currentuser/groups',
    type: "GET",
    async: false,
    contentType: "application/json;odata=verbose",
    headers: {
        "Accept": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val(),       
    },
    success: function(data){
        var groups = data.d.results
        for(i=0; i<groups.length; i++) {
            if (groups[i].LoginName == groupName) {               
               result = true;
            }
        };
    },
    error: function() { return false; }
  })
  return result;
}

checkUser = function() {
    if (IsCurrentUserMemberOfGroup("Template Test Members") == false) 
        $('nobr:contains("Feedback")').closest('tr').hide();
}
_spBodyOnLoadFunctionNames.push("checkUser");
</script>

To make fields read-only:

$("#fieldName").attr('readonly','readonly');

For choice field:

$("select[title^='Field Name']").attr('readonly', 'readonly');

Reference

  1. Hide SharePoint List Columns based on User Permissions.
6
  • Thank you Ganesh for you help. Just now even I have achieved it using SPServices. I will update my code as well.
    – Ravi
    Commented Feb 17, 2019 at 4:08
  • Welcome. Please accept and upvote if it helped you. Commented Feb 17, 2019 at 4:11
  • 1
    As opposed to hiding the field or making it read only for users NOT in the group, I hide the field first, then make it editable for users IN the group. This will ensure the field is always hidden in case the script for checking group membership doesn't run for some reason. Basically, err on the side of security.
    – AtariPixel
    Commented Feb 17, 2019 at 4:46
  • @AtariPixel Yes that's good for security but the requirement is to show data to all the users but only a few members should be able to edit it.
    – Ravi
    Commented Feb 17, 2019 at 4:54
  • 2
    @Ravi The concept is the same. 1. Make the field read only for all users. 2. Check if user is in group, if yes, make field editable.
    – AtariPixel
    Commented Feb 17, 2019 at 5:25
0

I was able to achieve this using SPServices with the below code :

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/2014.02/jquery.SPServices.js"></script>
<script type="text/javascript">
$(document).ready(function() {

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

//If the current User does belong to the group
if($(xData.responseXML).find("Group[Name='Vertical Owners']").length != 1)
{
    // Make the column readonly
    $("select[title='Status']").prop("disabled", true);
}
}
});
});
</script>
2
  • You don't have to use document.ready inside success function, I guess. Commented Feb 17, 2019 at 4:30
  • Yes that's right, missed that in hurry. Corrected it for the better performance. -Thanks Ganesh
    – Ravi
    Commented Feb 17, 2019 at 4:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.