CRUD operation on Users using REST

In the previous post we have seen CRUD operation on SharePoint Folders. In this post we are going to discuss about the operations on users in SharePoint Library using rest api.
Starting with the creation of Users class in our"RESTApiHelper" namespace.


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

Get all users in site

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

Get specific user by login id

    this.GetUserByLoginId = function (LoginId, doSuccess, doError) {
        jQuery.ajax({
            url: this.SiteUrl + "/_api/web/SiteUsers(@v)?@v='" + encodeURIComponent("i:0#.w|" + LoginId) + "'",
            method: "GET",
            headers: {
                "accept": "application/json; odata=verbose"
            },
            success: doSuccess,
            error: doError
        });
    }

Get specific user by email id

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

Get specific user by user id

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

Get user in  specific group by login id

    this.GetUserInGroupByLoginId = function (GroupId, LoginId, doSuccess, doError) {
        jQuery.ajax({
            url: this.SiteUrl + "/_api/web/sitegroups(" + GroupId + ")/users(@v)?@v='" + encodeURIComponent("i:0#.w|" + LoginId) + "'",
            method: "GET",
            headers: {
                "accept": "application/json; odata=verbose"
            },
            success: doSuccess,
            error: doError
        });
    }

Add user in group by login id

    this.AddUserIntoGroup = function (GroupId, LoginId, doSuccess, doError) {
        jQuery.ajax({
            url: this.SiteUrl + "/_api/web/sitegroups(" + GroupId + ")/users",
            method: "POST",
            data: JSON.stringify({ '__metadata': { 'type': 'SP.User' }, 'LoginName': 'i:0#.w|' + LoginId }),
            headers: {
                "accept": "application/json; odata=verbose",
                "content-type": "application/json; odata=verbose"
            },
            success: doSuccess,
            error: doError
        });
    }

Remove user from site by login id

    this.RemoveUserByLoginId = function (LoginId, doSuccess, doError) {
        jQuery.ajax({
            url: this.SiteUrl + "/_api/web/removebyloginname(@v)?@v='" + encodeURIComponent("i:0#.w|" + LoginId) + "'",
            method: "POST",
            headers: {
                "accept": "application/json; odata=verbose"
            },
            success: doSuccess,
            error: doError
        });
    }

Remove user from site by user id

    this.RemoveUserById = function (UserId, doSuccess, doError) {
        jQuery.ajax({
            url: this.SiteUrl + "/_api/web/removebyid(" + UserId + ")",
            method: "POST",
            headers: {
                "accept": "application/json; odata=verbose"
            },
            success: doSuccess,
            error: doError
        });
    }

Remove user from group by user id

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

Remove user from group by login id

    this.RemoveUserFromGroupByName = function (GroupId, LoginId, doSuccess, doError) {
        jQuery.ajax({
            url: this.SiteUrl + "/_api/web/sitegroups(" + GroupId + ")/removebyloginname(@v)?@v='" + encodeURIComponent("i:0#.w|" + LoginId) + "'",
            method: "POST",
            headers: {
                "accept": "application/json; odata=verbose"
            },
            success: doSuccess,
            error: doError
        });
    }
}

Click here to get the full script. This is it for the User object in SharePoint. In the next post we will work on Groups. 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