Saturday 15 October 2011

Delete user from Sharepoint 2010 Site using Javascript client object model

One of the ways to Remove a user from SharePoint site (using Javascript Object model) is to remove the user from the global User Information list of the current site.

Here is a simple example of how to remove a user (current user in our case) from User Information list using ECMAScript\JavaScript Client Object model.

function DeleteUser()
{

var clientContext = new SP.ClientContext.get_current();

var web = clientContext.get_web();

//Get User Info List
var userInfoList = web.get_siteUserInfoList();

context.load(userInfoList);

//Get Current user ID - Current UserId in a Site is the ID column of the user item in User Info List.
this._currentUser = web.get_currentUser();

context.load(this._currentUser);

var UserID = _currentUser.get_id();

var itemToDelete = userInfoList.getItemById(UserID);

itemToDelete.deleteObject();

clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded),Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded(sender, args)
{
alert("User Deleted");
}
function onQueryFailed(sender, args)
{
alert("Delete Failed");
}
}

1 comment:

  1. Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I can't wait to read lots of your posts.
    Sharepoint helpdesk

    ReplyDelete