3

I want to add a user to the members group of a SP site. Unfortunately I always get the above mentioned error when I use the LoginName format that is documented in the API reference. When I use simple UPN or email, I get User not unique or not found.

I should propably have added that I come from an outside source, i.e. our Servicenow Instance and authenticate through an OAUTH token.

Here is the REST call:
Endpoint: http://xyz.sharepoint.com/sites/TestSite/_api/web/sitegroups(5)/users

Header:

"Accept": "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose"

Content:

{ 
    '__metadata': { 'type': 'SP.User'},
    'LoginName':'i:0#.f|membership|[email protected]'
}

Result:

{"error":{"code":"-2147024809, System.ArgumentException","message":{"lang":"en-US","value":"Value does not fall within the expected range."}}}

Any ideas are welcome.

Here are some shots from the setup (within Servicenow):

Preprocess

Rest Step

Result

4
  • You have i:0#.f... written. Du you realy have FormsBased authentication? If yes, is "membership" the actual name of the issuer? please have a look at Claims Encoding at social.technet.microsoft.com/wiki/contents/articles/… Commented May 20, 2022 at 10:03
  • I got the username from a different API call looking up the user info, so I guess that is correct. Commented May 20, 2022 at 12:20
  • Is the user already known in the site collection ? If not you should first call /web/ensureuser Commented May 20, 2022 at 17:04
  • That said the http status code may help Commented May 20, 2022 at 17:06

3 Answers 3

2

Try using below code. I just tried this at my end and it is working for me:

<script type="text/javascript" src="/sites/siteName/SiteAssets/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>

You can check the LoginName format using site users endpoint. open below URL in browser tab:

https://tenant.sharepoint.com/sites/spconnect/_api/web/siteusers?$select=Title,LoginName

Example Output:

<m:properties>
    <d:LoginName>i:0#.f|membership|[email protected]</d:LoginName>
    <d:Title>Megan Bowen</d:Title>
</m:properties><m:properties>
    <d:LoginName>i:0#.f|membership|[email protected]</d:LoginName>
    <d:Title>Diego Siciliani</d:Title>
</m:properties>
4
  • Verify and use correct user LoginName and group ID from your SharePoint site. Commented May 20, 2022 at 10:28
  • I get both from previos API calls. xyz.sharepoint.com/_api/Web/GetUserById(ID) and .../_api/web?$expand=AssociatedOwnerGroup,AssociatedMemberGroup,AssociatedVisitorGroup Commented May 20, 2022 at 12:25
  • Did you try code given in my answer? Is it working for you? Try to copy data payload & headers as it. Commented May 20, 2022 at 12:26
  • I tried to replicate in my tool, not sure where I would run this script. See my edit in the original post. Commented May 20, 2022 at 12:41
1

The below code worked for me

var groupId = 6; //SharePoint group id
var userLoginName = 'i:0#.f|membership|[email protected]'; //Login name of the user to be added
$.ajax(
{
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/sitegroups/GetById("+ groupId +")/users",
    type: "POST",
    data: JSON.stringify(
    {
        '__metadata':
        {
            'type': 'SP.User'
        },
        'LoginName': userLoginName
    }),
    headers:
    {
        "Accept": "application/json;odata=verbose",
        "Content-Type": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
    },
    success: function(data, status, xhr)
    {
        console.log('Success');
    },
    error: function(xhr, status, error)
    {
        console.log("Failed");
    }
});

You can get the correct format/value for LoginName using the browser dev tools. Screenshot added below.

enter image description here

0

To use the REST capabilities that are built into SharePoint, you construct a RESTful HTTP request by using the OData standard, which corresponds to the client object model API you want to use.

The client.svc web service handles the HTTP request and serves the appropriate response in either Atom or JSON format.

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.