3

Is it possible to Create a new user in SharePoint? I tried the below but it doesn't work. We use ThinkTecture as the identity provider and not the default SharePoint authentication.

POST https://<siteurl>/_api/Web/Siteusers HTTP/1.1

POST BODY : {"__metadata":{"type":"SP.User"},"LoginName":"i:05.t|identity server|user@domain.com"}

I get a 404 - The user does not exist or is not unique.

5

You could try EnsureUser method to check whether the specified login name belongs to a valid user in the site. If the user doesn't exist, adds the user to the site.

Example

function ensureUser(accountName)
{
    var endpointUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/ensureUser('" + encodeURIComponent(accountName) + "')";
    return $.ajax({       
       url: endpointUrl,   
       type: "POST",  
       contentType: "application/json;odata=verbose",
       headers: { 
          "Accept": "application/json;odata=verbose",
          "X-RequestDigest": $("#__REQUESTDIGEST").val()
       }
    });
}


var accountName = 'i:0#.f|membership|jdoe@contoso.onmicrosoft.com';
ensureUser(accountName)
.done(function(data)
{
   console.log('Done.');   
})
.fail(function(error){
    console.log(JSON.stringify(error));
});
| improve this answer | |
  • 1
    Thanks Vadim :) I had already implemented the same. Thanks for your reponse :) – jkr May 20 '15 at 0:18
  • - For AD users: just use the AD username, not the domain neither anything else ( no need to encoudeURIcomponent ). - Ensure you add "charset=utf-8" to your headers ( content & accept ) – Alberto S. Dec 21 '17 at 11:21
0

It's actually only a comment on the answer of @VadimGremyachev, not intended as an answer, but it was too long for a comment.

Although I've not tested the suggestion of @Alberto (e.g. using the username without doamin for AD users), the life is not always so simple. For example, we have multiple domains, having several users with the very same user name in more than one of the domains (like domain1\jdoe, domain2\jdoe). If you should include the domain name as well, you will face issues you don't have with the ThinkTecture identity provider. In this case, the answer of @VadimGremyachev won't work. As the answer here suggest, you should URL encode only the membership provider part (like 'i:0#.w|') but not the user name (like 'domain\user'). Due to the backslash in the path, you get then the following error:

A potentially dangerous Request.Path value was detected from the client at System.Web.HttpRequest.ValidateInputIfRequiredByConfig()

Alternative solutions:

/_api/Web/EnsureUser(@v)?@v='i%3A0%23.w%7Cdomain\user'

or send the login name in the payload / request body without (!) encoding anything as suggested here:

function ensureUser(webUrl,loginName)
{
   var payload = { 'logonName': loginName }; 
   return $.ajax({
      url: webUrl + "/_api/web/ensureuser",
      type: "POST",
      contentType: "application/json;odata=verbose",
      data: JSON.stringify(payload),
      headers: {
         "X-RequestDigest": $("#__REQUESTDIGEST").val(),
         "accept": "application/json;odata=verbose"
      }
   });  
}


var loginName = 'i:0#.f|membership|jdoe@contoso.onmicrosoft.com'
ensureUser(_spPageContextInfo.webAbsoluteUrl,loginName)
.done(function(data)
{
    console.log('User has been added');
})
.fail(function(error){
    console.log('An error occured while adding user');
});

Remark: the same solutions should work for other identity providers (like ThinkTecture) as well, however, in cases where there is no (back)slash in the login name, the solution suggested by @VadimGremyachev provides a more simple approach.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.