0

I found a script to add a user to a SP group with a button. Now I would like to have a second button that does the opposite (remove the user from the group).

The first thing (adding an user) works. The second doesn't work. What am I doing wrong?

<script language="ecmascript" type="text/ecmascript">
var user;
var membersGroup;
function addUser2Members() {
     var clientContext = new SP.ClientContext();
     var groupCollection = clientContext.get_web().get_siteGroups();
     // Get the members group, assuming its ID is 22.
     membersGroup = groupCollection.getById(22);
     user = clientContext.get_web().get_currentUser();
     var userCollection = membersGroup.get_users();
     userCollection.addUser(user);
     clientContext.load(user);
     clientContext.load(membersGroup);
     clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded() {
    alert(' Congratulations!' + '\n \n ' + user.get_title() + ' \n \n' + ' Has successfully joined the SharePoint Group:' + '\n \n ' + membersGroup.get_title());
    SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK);
}
function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
function callAddUser() {
ExecuteOrDelayUntilScriptLoaded(addUser2Members, "sp.js");
}

function removeUserFromGroup() {
     var clientContext = new SP.ClientContext();
     var groupCollection = clientContext.get_web().get_siteGroups();
     // Get the members group, assuming its ID is 22.
     membersGroup = groupCollection.getById(22);
     user = clientContext.get_web().get_currentUser();
     var userCollection = membersGroup.get_users();
     userCollection.RemoveUser(user);
     clientContext.load(user);
     clientContext.load(membersGroup);
     clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded2), Function.createDelegate(this, this.onQueryFailed2));
}
function onQuerySucceeded2() {
    alert(' Congratulations!' + '\n \n ' + user.get_title() + ' \n \n' + ' Has successfully been deleted from the group:' + '\n \n ' + membersGroup.get_title());
    SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK);
}
function onQueryFailed2(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
function callRemoveUser() {
ExecuteOrDelayUntilScriptLoaded(removeUserFromGroup, "sp.js");
}

</script>
<input onclick="javascript:callAddUser();" type="button" value="Join Now!"/>
<input onclick="javascript:callremoveUser();" type="button" value="unsubscribe"/>

  • Are you getting any error while calling removeUserFromGroup function?? – Ganesh Sanap Nov 11 at 10:59
  • Nothing is happening. I don't get an failed alert neither – villefleurie Nov 11 at 11:12
  • JavaScript is case sensitive. Your function is called "callRemoveUser()" while your button onclick is called "callremoveUser()". The R must be uppercase in your input onclick event. – Christoffer Nov 11 at 11:26
  • I changed that point, but it still doesn't work – villefleurie Nov 11 at 11:29
  • Use F12 and see what/if you get any errors. And use breakpoints to debug! – Christoffer Nov 11 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.