CRUD operation on Groups using REST

In the previous post we have seen CRUD operation on SharePoint Users. In this post we are going to discuss about the operations on Groups in SharePoint Library using rest api.

Starting with the creation of Groups class in our"RESTApiHelper" namespace.

RESTApiHelper.Groups = function (SiteUrl) {
    this.SiteUrl = SiteUrl;


Get All groups in a Site
   
    this.GetAllGroups = function (doSuccess, doError) {
        jQuery.ajax({
            url: this.SiteUrl + "/_api/web/sitegroups",
            method: "GET",
            headers: {
                "accept": "application/json; odata=verbose"
            },
            success: doSuccess,
            error: doError
        });
    }

Get Group details by group id

    this.GetGroupById = function (GroupId, doSuccess, doError) {
        jQuery.ajax({
            url: this.SiteUrl + "/_api/web/sitegroups(" + GroupId + ")",
            method: "GET",
            headers: {
                "accept": "application/json; odata=verbose"
            },
            success: doSuccess,
            error: doError
        });
    }

Get Group details by group name

    this.GetGroupByName = function (GroupName, doSuccess, doError) {
        jQuery.ajax({
            url: this.SiteUrl + "/_api/web/sitegroups/getbyname('" + GroupName + "')",
            method: "GET",
            headers: {
                "accept": "application/json; odata=verbose"
            },
            success: doSuccess,
            error: doError
        });
    }

Get all users in a group by group name

    this.GetAllUsersInGroup = function (GroupId, doSuccess, doError) {
        jQuery.ajax({
            url: this.SiteUrl + "/_api/web/sitegroups(" + GroupId + ")/users",
            method: "GET",
            headers: {
                "accept": "application/json; odata=verbose"
            },
            success: doSuccess,
            error: doError
        });
    }

Create Group in a site

    this.CreateGroup = function (GroupName, doSuccess, doError) {
        jQuery.ajax({
            url: this.SiteUrl + "/_api/web/sitegroups",
            method: "POST",
            data: JSON.stringify({ '__metadata': { 'type': 'SP.Group' }, 'Title': GroupName }),
            headers: {
                "content-type": "application/json; odata=verbose"
            },
            success: doSuccess,
            error: doError
        });
    }

Click here to get the full script. This is it for the groups object in SharePoint. In the next post we will further explore the REST api. So stay tuned and enjoy, have fun with REST and JQuery !!!!

Comments

Post a Comment

Popular posts from this blog

Installation and Configuration of Office Online Server with SharePoint 2019

Workflow Manager 1.0- Installation, Configuration and Issues

SharePoint 2019 Upgrade Part 6: SharePoint Upgrade Issues