Javascript 使用getfilebyserverrelativeurl或getfolderbyserverrelativeurl时HTTP 400请求错误
我一直在尝试设置一个基本的CRUD操作示例SharePoint 2013应用程序 我有一个开发人员模板站点设置(例如reporting.domain.com/sites/dev)。在该网站内,我有一个“文档库”应用程序/列表设置和一个文件。我已在清单中授予“web”写入权限 我需要的是通过RESTAPI执行基本的CRUD操作。现在我正在尝试获取和删除操作。我收到400个“坏请求”。我完全卡住了。想法Javascript 使用getfilebyserverrelativeurl或getfolderbyserverrelativeurl时HTTP 400请求错误,javascript,rest,sharepoint,sharepoint-2013,Javascript,Rest,Sharepoint,Sharepoint 2013,我一直在尝试设置一个基本的CRUD操作示例SharePoint 2013应用程序 我有一个开发人员模板站点设置(例如reporting.domain.com/sites/dev)。在该网站内,我有一个“文档库”应用程序/列表设置和一个文件。我已在清单中授予“web”写入权限 我需要的是通过RESTAPI执行基本的CRUD操作。现在我正在尝试获取和删除操作。我收到400个“坏请求”。我完全卡住了。想法 <?xml version=\"1.0\" encoding=\"utf-8\"?>
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<m:error xmlns:m=\"http://schemas.microsoft.com/ado/2007/08/dataservices/metadata\">
<m:code>-2147024809, System.ArgumentException</m:code>
<m:message xml:lang=\"en-US\">Value does not fall within the expected range.</m:message>
</m:error>
-2147024809,System.ArgumentException
值不在预期范围内。
下面是我正在使用的javascript代码。gethostWebTitleAndCreated有效,但getReport或deleteReport均无效
(function () {
'use strict';
var hostweburl,
appweburl;
// Function to retrieve a query string value.
// For production purposes you may want to use
// a library to handle the query string.
function getQueryStringParameter(paramToRetrieve) {
var params, i, singleParam;
params = document.URL.split("?")[1].split("&");
for (i = 0; i < params.length; i = i + 1) {
singleParam = params[i].split("=");
if (singleParam[0] === paramToRetrieve) {
return singleParam[1];
}
}
}
function gethostWebTitleAndCreated() {
var url = appweburl + "/_api/SP.AppContextSite(@target)/web?@target='" + hostweburl + "'" + "&$select=title,created";
$.ajax(
{
url: url,
type: "GET",
headers: {
"accept": "application/json;odata=verbose",
"contentType": "application/json;odata=verbose"
},
success: function (data) {
$("<p>", {
text: data.d.Title
}).appendTo("#output");
},
error: function (err) {
$("<p>", {
text: JSON.stringify(err)
}).appendTo("#output");
}
}
);
}
function deleteReport() {
var executor = new SP.RequestExecutor(appweburl);
executor.executeAsync({
url: "../_api/SP.AppContextSite(@target)/web" +
"/getfilebyserverrelativeurl('/Custom Reports/NewReport.rdlx')" +
"?@target='" + hostweburl + "'",
method: "POST",
headers: {
"X-HTTP-Method": "DELETE",
"X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
"IF-MATCH": "*"
},
success: function () {
alert("Hurray!");
},
error: function (err) {
$("<p>", {
text: JSON.stringify(err)
}).appendTo("#output");
}
});
}
function getReport() {
var executor = new SP.RequestExecutor(appweburl);
executor.executeAsync({
url: appweburl + "/_api/SP.AppContextSite(@target)/web" +
"/getfilebyserverrelativeurl('/Custom Reports/NewReport.rdlx')/$value" +
"?@target='" + hostweburl + "'",
method: "GET",
headers: {
"accept": "application/json;odata=verbose",
"contentType": "application/json;odata=verbose"
},
success: function () {
alert("Hurray!");
},
error: function (err) {
$("<p>", {
text: JSON.stringify(err)
}).appendTo("#output");
}
});
}
// Load the required SharePoint libraries
$(document).ready(function () {
//Get the URI decoded URLs.
hostweburl =
decodeURIComponent(getQueryStringParameter("SPHostUrl"));
appweburl =
decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
// resources are in URLs in the form:
// web_url/_layouts/15/resource
var scriptbase = hostweburl + "/_layouts/15/";
// Load the js files and continue to the successHandler
$.getScript(scriptbase + "SP.RequestExecutor.js", gethostWebTitleAndCreated);
$("#getreport").on("click", getReport);
$("#deletereport").on("click", deleteReport);
});
}());
(函数(){
"严格使用",;
var hostweburl,
appweburl;
//函数检索查询字符串值。
//出于生产目的,您可能希望使用
//用于处理查询字符串的库。
函数getQueryStringParameter(paramToRetrieve){
变量参数,i,单参数;
params=document.URL.split(“?”[1]。split(“&”);
对于(i=0;i
端点的错误http:////_api/web/getFileByServerRelativeUrl(ServerRelativeEURL)
:
在找不到由serverRelativeUrl
参数指定的文件时发生
确保文件的serverRelativeUrl
参数指定正确,使用以下格式:
/<web>/<list>/<folder>/<file>
////
另一个解决方法(我也很惊讶地发现)是删除前面的/
字符
不起作用
工作
谢谢这让我有点吃惊,因为在每一个Microsoft文档中,它总是将路径显示为/folder/file。不幸的是,您并不孤单,当发现参数必须包括web url时,我也感到惊讶。谢谢您的回答。在我的例子中,我直接使用文件夹名而不是“/sites/”前缀。现在它成功了。这适用于GetFolderByServerRelativeUrl,但不适用于getFileByServerRelativeUrl。微软逻辑。。。
/<web>/<list>/<folder>/<file>
http://site url/_api/web/GetFolderByServerRelativeUrl('/Shared Documents')
http://site url/_api/web/GetFolderByServerRelativeUrl('Shared Documents')