Sharepoint REST API GetFolderByServerRelativePath在文件夹名称中使用百分号(%)时返回错误

时间:2018-01-29 11:38:12

标签: rest api sharepoint

我使用HTTP post方法上传文件,如下所示:

POST https://sitename.sharepoint.com/sites/subsitename/pagename/_api/web/getfolderbyserverrelativepath(decodedurl=@t)/files/addusingpath(overwrite=true,decodedurl=@f)?@t='docs/%24%26%2B%2C%3B%3D%40%23%25%7B++++%7D%5E%7E%5B%5D%60'&@f='new.docx' HTTP/1.1

    Authorization: "Bearer " + accessToken
    accept: "application/json;odata=verbose"
    content-type: "application/json;odata=verbose"

除了文件夹名称中包含百分号("%")的文件夹外,它适用于任何文件和文件夹。 如果目标文件夹名称包含百分号("%"),则返回错误消息:

{"error":{"code":"-2147024893, System.IO.DirectoryNotFoundException","message":{"lang":"en-US","value":"File Not Found."}}}

我找到了相关文章:https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/supporting-and-in-file-and-folder-with-the-resourcepath-api

因此,我尝试使用另一种post方法:

POST https://sitename.sharepoint.com/sites/subsitename/pagename/_api/web/getfolderbyserverrelativepath(decodedurl=@t)/files/addstubusingpath(decodedurl=@f)?@t='docs/%24%26%2B%2C%3B%3D%40%23%25%7B++++%7D%5E%7E%5B%5D%60'&@f='new.docx' HTTP/1.1

    Authorization: "Bearer " + accessToken
    accept: "application/json;odata=verbose"
    content-type: "application/json;odata=verbose"

但我得到了如下答复:

{"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"Not well formatted JSON stream."}}}

这里有什么问题?

2 个答案:

答案 0 :(得分:0)

以下是我与Fiddler的测试结果,您可以查看。

/sites/Developer/_api/web/getfolderbyserverrelativepath(decodedurl='/sites/Developer/MyDoc3/Fo%25lder')/Files/add(url='test.txt',overwrite=true)

enter image description here

答案 1 :(得分:0)

将空间符号编码为“%20”而不是“+”并使用下面的查询创建文件夹:

/_api/web/folders/AddUsingPath(decodedurl="folder%20name") 

然后通过查询将文件添加到该文件夹​​:

_api/web/getfolderbyserverrelativepath(decodedurl='docs/%24%26%2B%2C%3B%3D%40%23%25%7B%20%20%20%20%7D%5E%7E%5B%5D%60')/files/AddUsingPath(overwrite=true,decodedurl='new.docx')

现在工作正常。