Remove User Group Permission From SharePoint Site Using JSOM

In this article you will learn how to remove user group permission from SharePoint site using JSOM.

In this article I will share 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 document ready.
  • Get web from app context site.
  • Get RollAssignment from host web site.
  • Then delete the object using principle ID (User Group ID)
  • Finally group will be removed from SharePoint site as expected.

In your JavaScript file write the following code,

  1. // Js code Starts here   
  2. 'use strict';  
  3. //Get Current Context   
  4. var context = SP.ClientContext.get_current();  
  5. //Declaring the variables   
  6. varhostWebURL, appWebURL;  
  7. // this code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model   
  8. $(document).ready(function()  
  9.                     
  10.     {  
  11.     // Get App web URL and Host Web URL from Query string parameter   
  12.     hostWebURL = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));  
  13.     appWebURL = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'));  
  14.     //Calling remove group method in document ready   
  15.     RemoveGroup();  
  16. });  
  17. functionRemoveGroup()   
  18. {  
  19.     varcWeb = appCtx.get_web();  
  20.     varroleAssignments = cWeb.get_roleAssignments();  
  21.     //552 is id of user group  
  22.     roleAssignments.getByPrincipalId("552").deleteObject();  
  23.     context.load(cWeb);  
  24.     context.executeQueryAsync(function()   
  25.     {  
  26.         alert("success");  
  27.     }, function(sender, args)   
  28.     {  
  29.         alert("Request failed to change the group name " + args.get_message());  
  30.     });  
  31. }  

  32. //method for read query string value
    function manageQueryStringParameter(paramToRetrieve) {
    var params = document.URL.split("?")[1].split("&");
    var strParams = "";
    for (var i = 0; i < params.length; i = i + 1) {
    var singleParam = params[i].split("=");
    if (singleParam[0] == paramToRetrieve) {
    return singleParam[1];
    }
    }
    }



Summary

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