SharePoint: Remove User group permission from site using JSOM

In this article, we will see a way to remove the user group permission from SharePoint using JavaScript object model.

Steps for implementation:

  • Get Current Context.
  • Get App web URL and Host Web URL from query string parameter.
  • Calling RemoveGroup method in a document ready.
  • Get web from app context site.
  • Get RollAssignment from host web site.
  • Delete the object using principle ID (User Group ID)
  • Finally, the group will be removed from SharePoint site as expected.

In your JavaScript file write the following code:

// Js code Starts here 
  
'use strict';
  
//Get Current Context 
  
var context = SP.ClientContext.get_current();
  
//Declaring the variables 
  
var hostWebURL, appWebURL;
  
// this code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model 
  
$(document).ready(function()
  
{
  
    // Get App web URL and Host Web URL from Query string parameter 
  
    hostWebURL = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
  
    appWebURL = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'));
  
    //Calling remove group method in document ready 
  
    RemoveGroup ();
  
});
  
function RemoveGroup()
  
{
  
    var cWeb = appCtx.get_web();  
  
    var roleAssignments = cWeb.get_roleAssignments();
  
   //552 is id of user group
  
    roleAssignments.getByPrincipalId("552").deleteObject(); 
  
    context.load(cWeb);
  
    context.executeQueryAsync(function () {
  
        alert("success");
  
    }, function (sender, args) {
  
        alert("Request failed to change the group name " + args.get_message());
  
    });
  
}

Summary

In this article we have explored how to remove the user group permission from SharePoint using JavaScript object model.