-1

Hi I followed another thread but cannot get the following code to work. I changed Line 8 from web to oWeb, error seems to be around line 8/9

I'm trying to remove a user from a group.

    function removeUserFromGroup()
    {

    var clientContext = new SP.ClientContext.get_current();
    var oWeb = clientContext.get_web()
    var collGroup = oWeb.get_siteGroups();
    var oGroup = collGroup.getById(25); //Change 7 based on the group's id
    var oUser = oGroup.get_users();
    clientContext.load(oUser);
 8)   var user = oWeb.ensureUser('DOM1\jon');

 9)   oUser.removeByLoginName(user.get_loginName());   

    clientContext.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded7), Function.createDelegate(this, onQueryFailed7));   

    }


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

    function onQueryFailed7() {
        alert('Failed');
    }

UPDATE

If I comment ensureUser ByLoginName lines it executes.

I've now change last two lines to the following and seems to work but I'd rather use Account 'domain\username'

var user = oWeb.ensureUser('Sharepoint Jon'); oUser.remove(user);

3
  • What error you are getting? Can you check if user is null or not? May 15, 2015 at 16:11
  • Is the group you are trying to access readable by everyone or at least the user executing the code and is the user executing the code able to modify permissions normally? May 15, 2015 at 16:11
  • Error: The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested. May 18, 2015 at 7:34

1 Answer 1

0

You are attempting to use a property that has not been loaded yet. Line 9 contains user.get_loginName(). Before you can use this, you need to load it first:

clientContext.load(user, 'Include(LoginName)');
clientContext.executeQueryAsync(function(){
    oUser.removeByLoginName(user.get_loginName());
    clientContext.executeQueryAsync(
        Function.createDelegate(this, onQuerySucceeded7),
        Function.createDelegate(this, onQueryFailed7)
    );
}, function(args) { alert(args.get_message()); } );

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.