这个问题是我作为起点发现的这个问题的扩展(无需特殊字符即可工作):
SharePoint REST query SP.UserProfiles.PeopleManager
基本上,我遇到的问题是查询无法准确响应带有特殊字符的accountName
。具体来说,此示例的姓氏中包含一个'
。该查询不返回任何结果,或者是400错误的请求。
在代码示例中,我使用了encodeURIComponent()
,但是我也尝试了escape()
和字符串转义"\"
。
在这一点上,我假设它是MS方面的一个错误,但是我找不到任何支持文档,也找不到任何成功完成此操作的代码示例。
var siteUrl = _spPageContextInfo.siteAbsoluteUrl;
var accountName = "Domain\\LoginFirstName_O'AccountLastName";
$.ajax({
url: siteUrl + "/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v='" + encodeURIComponent(accountName) + "'",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
console.log(data);
},
error: function (data) {
console.log(JSON.stringify(data));
}
});
最佳答案
显然,我比我想像的更接近答案,但我却忽略了它。基本上,在这种情况下,SharePoint的转义方法有效。我需要添加代码以将单个'
替换为''
。
我还发现,不管encodeURIComponent()
为何,该请求均对其进行编码,因此对于这一请求,我选择将其删除。是否使用它取决于您。
这是我的最终代码段:
var siteUrl = _spPageContextInfo.siteAbsoluteUrl;
var accountName = "Domain\\LoginFirstName_O'AccountLastName";
accountName = accountName.replace("'","''");
$.ajax({
url: siteUrl + "/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v='" + accountName + "'",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
console.log(data);
},
error: function (data) {
console.log(JSON.stringify(data));
}
});
关于javascript - SharePoint REST查询SP.UserProfiles.PeopleManager特殊字符,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38793930/