Question

I've tried and tried other options, and cannot come up with a solution to this one that actually works, so I wanted to put this one out there for help.

As the title describes, I'm trying to disable (not hide, but make READ-ONLY (but visible)) a couple of fields in my SP list Edit form. I need this to be done based on which permissions group the user is in. (i.e. - if they're NOT in the LEADERS group, the fields are READ ONLY. If they ARE in the LEADERS group, the fields are fully editable)

Here's the code I'm pounding over now:

<script>
function HideFields() {

//these are the fields in question. Origin is a single text line, Approved is a check box

    fieldsToHide = ["Origin", "Approved"]; 


    var formFieldTitles = $(".ms-formtable td.ms-formlabel h3.ms-standardheader");


    formFieldTitles.each(function () {

        var textToMatch = $(this).text();

        var currentRow = $(this).closest('tr');


        for (var i = 0; i < fieldsToHide.length; i++){
            var field = fieldsToHide[i];

            if (textToMatch.toLowerCase().replace("*", "").trim() === field.toLowerCase().replace("*", "").trim()){

                $(currentRow).disable();
            }
        }
    });
}

function AddToBodyOnLoad(){

    _spBodyOnLoadFunctionNames.push("HideFields");
}


_spBodyOnLoadFunctionNames.push("AddToBodyOnLoad");

Was it helpful?

Solution

Use the following scripts to make fields read-only:

<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js"></script> 
<script type="text/javascript">
 $(document).ready(function()
{
//Set the Field to Read only and change its background colour
$("input[title='Origin']").attr("readonly","true").css('background-color','#F6F6F6');
//You can also disable the field using:  
$("input[Title='Approved']").attr("disabled", "disabled");
});
</script>

Use SPWeb.IsCurrentUserMemberOfGroup method to check if the current user is a member of the specified group. And then disable the fields based on the check result.

Demos:

Check if user is in a specified group

Check if current user is member of a SharePoint group using JQuery

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top