当文件名中有#符号时,Sharepoint REST API GetFileByServerRelativeUrl

时间:2017-12-23 16:10:08

标签: rest api sharepoint sharepoint-online

当文件名中有#符号时,我在使用REST API从Sharepoint获取文件时遇到问题,我将其转义为%23,但响应是找不到文件(404)。看: https://dev.office.com/blogs/upcoming-changes-to-sharepoint-and-onedrive-for-business-apis-to-support-and-in-file-names

他们解释说GetFileByServerRelativeUrl API不支持#和%符号,但是没有解释REST API如何使用文件名中包含#的文件。

由于

1 个答案:

答案 0 :(得分:0)

实际上,即使Microsoft已经introduced a support for special characters,但SP.Web.getFileByServerRelativeUrl method在文件名包含#字符时失败(无论#是否转义为%23或不)

但您可以考虑使用以下端点来检索文件属性:

Url: http://<sitecollection>/<site>/_api/web/folders/getbyurl(folderrelativeurl)/files/getByUrl(url)
Method: GET

例如:

var folderUrl = "Shared Documents";
var fileName = "Guide #123.docx";

var requestUrl = `${_spPageContextInfo.webAbsoluteUrl}/_api/web/folders/getbyurl('${encodeURIComponent(folderUrl)}')/files/getByUrl('${encodeURIComponent(fileName)}')`;
$.getJSON(requestUrl)
.then(function(data){
    console.log(data.Name);
})
.fail(function(error){
    console.log(error.responseText);
});