SP.GroupCollection object

Represents a collection of SP.Group objects.

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

var object = new SP.GroupCollection()

Members

The GroupCollection object has the following members.

Constructor

The GroupCollection object has the following constructor.

Constructor

Description

GroupCollection

Initializes a new instance of the SP.GroupCollection object.

Methods

The GroupCollection object has the following methods.

Method

Description

add

Adds a group to the collection of groups in a site collection.

getById

Returns a group from the collection based on the member ID of the group.

getByName

Returns a cross-site group from the collection based on the name of the group.

itemAt

remove

Removes the specified group from the collection.

removeById

Removes the group with the specified member ID from the collection.

removeByLoginName

Removes the cross-site group with the specified name from the collection.

Properties

The GroupCollection object has the following properties.

Property

Description

childItemType

item

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 GroupCollection resource for more information.

Endpoint URI structure

http://<sitecollection>/<site>/_api/web/sitegroups

HTTP requests

This resource supports the following HTTP commands:

GET syntax

GET http://<sitecollection>/<site>/_api/web/sitegroups

POST syntax

POST http://<sitecollection>/<site>/_api/web/sitegroups