0

I am trying to add a user to the SharePoint group from the REST API in SharePoint 2013.

It says successfully added but never adds the user

    function addUserToGroup(userloginname) {
    var addUserToGroupEndpoint =  "https://mysite/_api/web/sitegroups(12061)/users";

    var payload = JSON.stringify({
        '__metadata': { 'type': 'SP.User' },
        'LoginName': ''+userloginname+''
    });

    $.ajax({
        url: addUserToGroupEndpoint,
        type: "POST",
        data: payload,
        headers: {
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose"
        },
        success: function (data) {
            alert("User Added to Group successfully!");
        },
        error: function (error) {
            alert(error + "Inform admins");
        }
    });
}

I am running out of ideas, if anyone can help.

1 Answer 1

1

Here's the working SharePoint REST API example to add user to SharePoint group:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>

<button type="button" onclick="addUserToGroup()"> Add User To Group </button>

<script type="text/javascript">
    function addUserToGroup() {
        var addUserToGroupEndpoint = _spPageContextInfo.webAbsoluteUrl + "/_api/web/sitegroups(38)/users";

        var payload = JSON.stringify({
            '__metadata': { 'type': 'SP.User' },
            'LoginName': 'i:0#.f|membership|[email protected]'
        });

        $.ajax({
            url: addUserToGroupEndpoint,
            type: "POST",
            data: payload,
            headers: {
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                "accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose"
            },
            success: function (data) {
                console.log("User Added to Group successfully!");
            },
            error: function (error) {
                console.log(error);
            }
        });
    }
</script>

Make sure you are using correct login name of user and the ID of your SharePoint group.

I just tried this and it is working fine for me:

enter image description here

6
  • i am getting 404 error for this line "mysite/_api/web/sitegroups(12061)/users i am the site collection admin for the site. Commented May 25, 2023 at 14:58
  • it worked for a while then all of a sudden I get this failed to load resource error on the button click, but when I click on the link then i am able to see the group details Commented May 25, 2023 at 15:26
  • Are you using correct group ID, can you confirm it once? Commented May 25, 2023 at 16:12
  • yeah, I am I can browse in individually mysite/_api/web/sitegroups(12061)/users but when my function is calling it on the button click it says 404. Commented May 25, 2023 at 16:38
  • can you add the updated code you are using currently to your question by editing original question? Is there any detailed error message with 404 code? Commented May 25, 2023 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.