SP.UserCollection.addUser Method (sp.js)

Adds the specified user to the collection.

Applies to: apps for SharePoint | SharePoint Foundation 2013 | SharePoint Server 2013



  • user
    Type: SP.User

    The user to add.

Return value

Type: SP.User

The new user.


The following example creates an Input button on an application page that adds the current user to the visitors group on the current Web site.

<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();

     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());


    <input id="Button1" type="button" value="Run Code" onclick="runCode()" />