0

I have figured out how to break inheritance on a site, but I really want to do that and then remove all the standard and/or inherited user groups from the site.

Is there a way to iterate through all existing user groups in javascript and removing them?

2
  • you want to remove users or groups? Commented Aug 8, 2014 at 9:59
  • I want to remove the all user groups from the current website (web in JS terms). Commented Aug 8, 2014 at 10:17

1 Answer 1

2

How to delete SP.Group in SharePoint 2013 via JSOM

Use SP.GroupCollection.remove, SP.GroupCollection.removeById or SP.GroupCollection.removeByLoginName methods to remove Group from site.

Example

var context = SP.ClientContext.get_current();
var groups = context.get_web().get_siteGroups();
groups.removeById(groupId);

context.executeQueryAsync(
    function() {
          console.log('Group is deleted');
    },
    function(sender, args) {
           console.log(args.get_message());
    }    
);

In your case it is not clear why you need to remove all the groups where user belongs to, but the following script demonstrates how to perform that:

var context = SP.ClientContext.get_current();
var user = context.get_web().getUserById(userId);
context.load(user,'Groups');
context.executeQueryAsync(
   function() {
       var groups = user.get_groups();
       for(var i = 0; i < groups.get_count();i++) {
           var group = groups.getItemAtIndex(i);
           groups.removeByLoginName(group.get_loginName());
       }


       context.executeQueryAsync(
          function() {
             console.log('Completed'); 
          },
          function(sender, args) {
             console.log(args.get_message());
          }    
       );


    },
    function(sender, args) {
        console.log(args.get_message());
    }    
);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.