Sharepoint 2013:通过REST API确保用户

时间:2015-10-21 12:39:45

标签: json ajax rest sharepoint sharepoint-2013

我正在尝试通过REST API自动确保一些用户。 我的REST电话:

$.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));
}
});

现在发送此电话时,我收到以下错误;

“错误请求:Microsoft.Data.OData.ODataContentTypeException找不到与响应的内容类型匹配的受支持的MIME类型。所支持的类型'application / json; odata = verbose'都不匹配内容类型'application / x-www-form-urlencoded; charset = UTF-8'“

建立的呼叫类似in the msdn reference

2 个答案:

答案 0 :(得分:5)

发生此错误,因为需要明确指定sentence,因为它是JSON请求:

  

ContentType(默认:' application / x-www-form-urlencoded;   字符集= UTF-8&#39)

     

将数据发送到服务器时,请使用此内容类型。默认是   "应用程序/ X WWW的窗体-urlencoded;字符集= UTF-8"

示例

contentType

答案 1 :(得分:2)

替代解决方案:

您还可以通过以下方式执行REST查询:

        $.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 将包含目标AD用户名。

注意:

  • AD用户使用 ensureUser 方法时,只需使用 用户名即可。因此, user_name 将是这样的:"用户名"。没有 需要添加"域\"之前没有任何其他类型的前缀/后缀。 只需用户名

  • ContentType&接受标题必须是 application / json; odata = verbose; charset = utf-8