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

问题描述 投票:1回答:1

当文件名中有#符号时,我在使用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中如何使用文件名中包含#的文件。

谢谢

rest api sharepoint sharepoint-online
1个回答
0
投票

事实上,即使微软已经有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);
});
© www.soinside.com 2019 - 2024. All rights reserved.