Disable document edit properties based on user permission in SharePoint document library

This SharePoint tutorial, we will discuss how to disable document edit properties based on user permissions in SharePoint document library using SPServices and sputility js. The code will work in SharePoint Online as well as SharePoint 2013/2016.

Here I have a SharePoint document library which has a metadata column as “Department” which is a choice column. Here my requirement is, whenever a user tries to change the Department, then it will check if the user belongs to “Finance Team Approvers” SharePoint group or not.

Disable document edit properties based on user permission in SharePoint document library

Here we will do this by using client-side code like SPServices and sputility.js.

Open the library edit form and add a script editor web part in the page.

Example:

https://onlysharepoint2013.sharepoint.com/sites/SharePointSky/TestDocumentLibrary/Forms/EditForm.aspx

Below is the full code which will trigger on dropdown selected change. And first we are keeping the old selected value and if the user does not belong to the SharePoint group, then we will set the old value in the dropdown.

<script src="https://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/0.7.1a/jquery.SPServices-0.7.1a.min.js"></script>
 <script src="https://onlysharepoint2013.sharepoint.com/sites/SharePointSky/SiteAssets/sputility.min.js"></script>

<script type="text/javascript">
_spBodyOnLoadFunctionNames.push("ourFunctionName");    

function ourFunctionName()
{    
    	var oldvalue=$("select[Title='Department']").val();  
	$("Select[Title='Department']").change(function () {

        $().SPServices({
            operation: "GetGroupCollectionFromUser",
            userLoginName: $().SPServices.SPGetCurrentUser(),
            async: false,
            completefunc: function (xData, Status) {                
                var fta = "false";
              
                if (($(xData.responseXML).find("Group[Name='Finance Team Approvers']").length > 0)) {
                    fta = "true";
                }
                            
	var newValue=$("select[Title='Department']").val();

	if (fta == "true") {

                    }
                    else {
                        alert("You are not authorized to change to the status");
                        $("select[Title='Department']").val(oldvalue);
                    }                                                       
            }
        });

    });
    }
    
</script>

Save the above code.

Now, I logged with a user who is not a part of the SharePoint group (Finance Team Approvers), when the user tries to change the department, it will show a message, you are not authorized to change the status. And also, the drop-down selected value will reset it to the previously selected value in the edit form.

Disable document edit properties based on user permission in SharePoint document library
Disable document edit properties based on user permission in SharePoint document library

Disable document edit properties based on user permission in SharePoint document library (Page load)

The above code will check in the dropdown selected changed event, but sometimes we need to check this in the page load itself.

So I have modified the code little bit so that it will check if the user belongs to the particular SharePoint group in the page load itself. And if the user does not belong to the SharePoint group then it will go back to the previous page.

<script src="https://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/0.7.1a/jquery.SPServices-0.7.1a.min.js"></script>
 <script src="https://onlysharepoint2013.sharepoint.com/sites/SharePointSky/SiteAssets/sputility.min.js"></script>

<script type="text/javascript">

$(document).ready(function () {
ExecuteOrDelayUntilScriptLoaded(ourFunctionName, "sp.js");
});
 
function ourFunctionName()
{    
    	var oldvalue=$("select[Title='Department']").val();  
	
        $().SPServices({
            operation: "GetGroupCollectionFromUser",
            userLoginName: $().SPServices.SPGetCurrentUser(),
            async: false,
            completefunc: function (xData, Status) {                
                var fta = "false";
              
                if (($(xData.responseXML).find("Group[Name='Finance Team Approvers']").length > 0)) {
                    fta = "true";
                }
                            
	var newValue=$("select[Title='Department']").val();

	if (fta == "true") {

                    }
                    else {
                        alert("You are not authorized to change to the status");
                        $("select[Title='Department']").val(oldvalue);
						window.history.back();
                    }                                                       
            }
        });   
    }
    
</script>

You may like following SharePoint tutorials:

Hope this SharePoint tutorial explains how to disable document edit properties based on user permission in SharePoint document library.

  • >