SP.UserCollection.addUser(user) Method
Applies to: SharePoint Foundation 2010
Adds the specified user to the collection.
var value = SP.UserCollection.addUser(user);
Parameters
user
Type: SP.User
The user to add.
Return Value
Type: SP.User
The new user.
Applies To
Example
The following example creates an Input button on an application page that adds the current user to the visitors group on the current website.
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<script type="text/ecmascript" language="ecmascript">
var user;
var visitorsGroup;
function runCode() {
var clientContext = new SP.ClientContext();
var groupCollection = clientContext.get_web().get_siteGroups();
// Get the visitors group, assuming its ID is 4.
visitorsGroup = groupCollection.getById(4);
user = clientContext.get_web().get_currentUser();
var userCollection = visitorsGroup.get_users();
userCollection.addUser(user);
clientContext.load(user);
clientContext.load(visitorsGroup);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded() {
alert(user.get_title() + " added to group " + visitorsGroup.get_title());
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
<input id="Button1" type="button" value="Run Code" onclick="runCode()" />
</asp:Content>