Tuesday, 29 September 2015

Create Group Using SharePoint 2013 REST API

function createSharepointGroup() {
    var groupName = [];
    groupName.push({ title: 'Admin', desc: "Admin Groups users in this group has full rights" });
    groupName.push({ title: 'Operation', desc: "Operation Group" });
    groupName.push({ title: 'Users', desc: "Users Group" });
    for (var i = 0; i < groupName.length; i++) {
        var spGroup = {
            "__metadata": {
                "type": "SP.Group"
            },
            "Title": groupName[i].title,
            "Description": groupName[i].desc,
        };

        $.ajax({
            url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/SiteGroups",
            type: "POST",
            contentType: "application/json;odata=verbose",
            data: JSON.stringify(spGroup),
            headers: {
                "Accept": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val()
            },
            success: function (data) {
                //success(data);
            },
            error: function (data) {
                //failure(data);
            }
        });
    }

}