-1

I'm trying to remove a user from a SharePoint 2010 group using JSOM, but not having much luck. I've been trying to adapt the script on this page - http://www.c-sharpcorner.com/Blogs/9650/add-and-remove-user-into-sharepoint-group-using-client-objec.aspx

When I run my code, I get an error about line 8 - "Unexpected token =>" I see other examples with that notation. What's wrong with it?

function removeFromGroup(siteURL) {    
    var client = new ClientContext(siteURL);
    client.ExecuteQuery();
    var website = client.Web;
    client.Load(website, w => w.AllProperties, w => w.SiteGroups, w => w.SiteUserInfoList, w => w.Webs,w => w.Title);
    client.ExecuteQuery();
    var groupCollection = website.SiteGroups;
    client.Load(groupCollection, groups => groups.Include(grps => grps.Users, grps => grps.Title));
    client.ExecuteQuery();
    var user;
    foreach (Group group in groupCollection)
    {
        if (group.Title.Equals("Test"))
        {
            var userInfo = new UserCreationInformation();
            userInfo.LoginName = "<I put the AD username here>";

            user = group.Users.GetByLoginName("<I put the AD username here>");
            group.Users.Remove(user);
            group.Update();
            website.Update();
            client.ExecuteQuery();
        }
    }
}

1 Answer 1

1

The code you posted is CSOM and doesn't work with JavaScript

Using JavaScript you can do as follows

var clientContext = new SP.ClientContext(siteUrl);
var collGroup = clientContext.get_web().get_siteGroups();
clientContext.load(collGroup);
clientContext.load(collGroup, 'Include(Users)');

clientContext.executeQueryAsync(function (sender, args) {
    var groupEnumerator = collGroup.getEnumerator();
    while (groupEnumerator.moveNext()) {
        var oGroup = groupEnumerator.get_current();
        var collUser = oGroup.get_users();
        var userEnumerator = collUser.getEnumerator();
        while (userEnumerator.moveNext()) {
            if(oGroup.get_title() == "Test") {
                var oUser = userEnumerator.get_current();
                if(oUser.get_loginName() == "userlogin") {
                    oGroup.get_users().remove(oUser);
                    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
                }
            }
        }
    }
}, function (sender, args) {
    alert('Query failed');
});

function onQuerySucceeded() {
    alert('User Removed');
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.