问题
This question is an extension of this one I found as a starting point (which works without special characters): SharePoint REST query SP.UserProfiles.PeopleManager
Basically the problem I am having is the query does not accurately respond to accountName
with special characters. Specifically, a '
in the last name for this example. The query either returns no results or is a 400 Bad Request.
In the code example I have used encodeURIComponent()
, but I have also tried escape()
and string escapes "\"
.
At this point I am assuming its a bug on the MS side, but I cannot find any support documentation, nor any examples of code that have done this successfully.
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));
}
});
回答1:
Apparently I was closer to the answer than I thought, but I overlooked it. Basically, SharePoint's way of escaping worked in this case. I needed to add code to replace the single '
with ''
.
I also found that the request encodes it regardless of the encodeURIComponent()
, so for this one I opted to remove it. Up to you if you want to use it or not.
Here is my final code snippet:
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));
}
});
来源:https://stackoverflow.com/questions/38793930/sharepoint-rest-query-sp-userprofiles-peoplemanager-special-characters