Sharepoint 2013: EnsureUser via REST API

╄→гoц情女王★ 提交于 2019-12-18 18:20:11

问题


I'm trying to ensure some users automatically via REST API. My REST call:

$.ajax({
url: "blablabla/_api/web/ensureuser",
type: "POST",
data: "{ 'logonName': 'i%3A0%23.w%7Cdomain%09logonName' }",
headers: {
    "X-RequestDigest": $("#__REQUESTDIGEST").val(),
    "accept": "application/json;odata=verbose"
},
success: function () {
    console.log("done!");
},
error: function (err) {
    console.log(JSON.stringify(err));
}
});

Now when sending this call I get the following error;

"Bad Request: Microsoft.Data.OData.ODataContentTypeException A supported MIME type could not be found that matches the content type of the response. None of the supported type(s) 'application/json;odata=verbose' matches the content type 'application/x-www-form-urlencoded; charset=UTF-8'"

The call is built like in the msdn reference specified.


回答1:


This error occurs since ContentType needs to be specified explicitly since it's a JSON request:

contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8')

When sending data to the server, use this content type. Default is "application/x-www-form-urlencoded; charset=UTF-8"

Example

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');
});



回答2:


ALTERNATIVE SOLUTION:

You could also perform a REST query in the following way:

        $.ajax({
        url: "http://[website]/_api/web/ensureuser('"+user_name+"')",
        type: "POST",
        headers: {             
            'accept': 'application/json;odata=verbose;charset=utf-8',
            'Content-Type': 'application/json;odata=verbose;charset=utf-8',
            'X-RequestDigest': $("#__REQUESTDIGEST").val()                    
        },
        success: function(response_data){ [your custom success action]
        },
        error: function(response_data){[your custom fail action]}
      });

var user_name will contain target AD username.

NOTE:

  • When using the ensureUser method against an AD user, just use its username. So, user_name would be something like this: "username". No need to add "domain\" before neither any other kind of prefix/suffix. Just the username

  • ContentType & accept headers MUST be application/json;odata=verbose;charset=utf-8



来源:https://stackoverflow.com/questions/33259576/sharepoint-2013-ensureuser-via-rest-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!