SP.UserCollection object

Represents a collection of SP.User object.

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

var object = new SP.UserCollection()

Members

The UserCollection object has the following members.

Constructor

The UserCollection object has the following constructor.

Constructor

Description

UserCollection

This member is reserved for internal use and is not intended to be used directly from your code.

Methods

The UserCollection object has the following methods.

Method

Description

add

Adds the user specified by the user creation information parameter to the collection.

addUser

Adds the specified user to the collection.

getByEmail

Gets the user with the specified email address.

getById

Gets the user with the specified member identifier (ID).

getByLoginName

Gets the user with the specified login name.

itemAt

Gets the user at the specified index of the collection.

remove

Removes the specified user from the collection.

removeById

Removes the user with the specified ID.

removeByLoginName

Removes the user with the specified login name.

Properties

The UserCollection object has the following properties.

Property

Description

childItemType

item

Gets the user at the specified index of the collection.

Example

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

REST resource endpoint

See UserCollection resource for more information.

Endpoint URI structure

http://<sitecollection>/<site>/_api/web/sitegroups(groupid)/users

HTTP requests

This resource supports the following HTTP commands:

GET syntax

GET http://<sitecollection>/<site>/_api/web/sitegroups(groupid)/users

POST syntax

POST http://<sitecollection>/<site>/_api/web/sitegroups(groupid)/users