【问题标题】:Sharepoint 2013: EnsureUser via REST APISharepoint 2013:通过 REST API 确保用户
【发布时间】:2016-01-20 11:10:45
【问题描述】:

我正在尝试通过 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

【问题讨论】:

    标签: json ajax rest sharepoint sharepoint-2013


    【解决方案1】:

    出现此错误是因为ContentType 需要明确指定,因为它是 JSON 请求:

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

    向服务器发送数据时,使用此内容类型。默认为 "application/x-www-form-urlencoded; charset=UTF-8"

    示例

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

    【讨论】:

    • 缺少 contentType 的部分是正确的 - 让它与它一起工作。 (从什么时候开始的 msdn 引用错误?:O)不过我不建议使用这个代码示例 - 缺少错误处理程序会使开发变得非常糟糕。
    • fail 函数在我的示例中用于处理错误,您始终可以从error 响应对象中获取错误详细信息
    • 哎呀,不知道。对不起。
    • 我在确定登录名的正确语法时遇到了一点问题。如果我在 Sharepoint 中的登录名通常是 i:0#.w|dom\userX" 我将如何为 ensureUser-Method 设置登录名?@website 是否与 SPS-UserPrincipalName 中指定的地址相同?
    • 对于 AD 用户:只使用 AD 用户名,不要使用域,也不要使用其他任何东西。确保在标题中添加“charset=utf-8”
    【解决方案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

    【讨论】:

    • 在我的错误消失之前,我必须像这个解决方案建议的那样将 Content-Type 添加到标题中。
    猜你喜欢
    • 2016-08-31
    • 2013-06-06
    • 2014-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-23
    • 2015-11-30
    • 2016-01-08
    相关资源
    最近更新 更多