I want to use SharePoint REST API to get and operate the permission value of a specific folder in Shared Documents.

大坂 翔 141 Reputation points
2021-08-17T07:16:13.69+00:00

I want to use SharePoint REST API to get and operate the permission value of a specific folder in Shared Documents.

The SharePoint REST API docs had a site-wide role permission operation "https: // <servername> / site / _api / web / RoleAssignments", but there is no mention of the specific folder access operation I want to do this time. was.

How do I make an HTTP request to change the permissions on a particular folder?
If you are familiar with this, please answer.

Microsoft 365 and Office SharePoint Development
Microsoft 365 and Office SharePoint For business Windows
Microsoft 365 and Office SharePoint Server Development
0 comments No comments
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2021-08-18T06:42:49.737+00:00

    Hi @大坂 翔 ,
    First we need to break role inheritance for the folder.

    function breakRoleInheritanceOfFile() {   
    	 $.ajax({  
    			url: siteurl + String.format("/_api/web/GetFolderByServerRelativeUrl('DocumentTest/test999')/ListItemAllFields/breakroleinheritance(true)"),  
    			type: "POST",  
    			contentType: "application/json;odata=verbose",  
    			headers: {  
    				"Accept": "application/json;odata=verbose",  
    				"X-RequestDigest": $("#__REQUESTDIGEST").val()  
    			},  
    			success: onQuerySucceeded,  
    			error: onQueryFailed  
    	 });     
    }  
    

    Remove the current role assignment for the folder

    function remove(){  
    	$.ajax({  
    			url: siteurl + String.format("/_api/web/GetFolderByServerRelativeUrl('DocumentTest/test999')/ListItemAllFields/roleassignments/getbyprincipalid(11)"),  
    			method: "POST",  
    			headers: {  
    				   Authorization: "Bearer " + accessToken,  
    				   "accept": "application/json;odata=verbose",  
    				   "content-type": "application/json;odata=verbose",  
    				   "X-HTTP-Method": "DELETE"  
    			},  
    			success: onQuerySucceeded,  
    			error: onQueryFailed  
    	});   
    }  
    

    Add the new role assignment for the folder

    function add(){  
    	$.ajax({  
    			url: siteurl + String.format("/_api/web/GetFolderByServerRelativeUrl('DocumentTest/test999')/ListItemAllFields/roleassignments/addroleassignment(principalid=11,roledefid=1073741829)"),  
    			method: "POST",  
    			headers: {  
    				   Authorization: "Bearer " + accessToken,  
    				   "accept": "application/json;odata=verbose",  
    				   "content-type": "application/json;odata=verbose"  
    			},  
    			success: onQuerySucceeded,  
    			error: onQueryFailed  
    	}); 	  
    }  
    

    In order to use this API we need 'FullControl' permissions,So we need get Access Token by app


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.