gam*_*r42 2 javascript rest sharepoint sharepoint-2013
我一直在尝试设置基本的CRUD操作示例SharePoint 2013 App.
我有一个开发人员模板站点设置(例如reporting.domain.com/sites/dev).在该网站内,我有一个带有文件的"文档库"应用程序/列表设置.我在清单中给出了"web"写入权限.
我需要的是通过REST API执行基本的CRUD操作.现在我只是尝试获取和删除操作.我收到400"错误请求".我完全陷入困境.想法?
<?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>
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的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);
});
}());
Run Code Online (Sandbox Code Playgroud)
端点错误http://<sitecollection>/<site>/_api/web/getFileByServerRelativeUrl(serverRelativeUrl):
Value does not fall within the expected range.
Run Code Online (Sandbox Code Playgroud)
serverRelativeUrl无法找到参数指定的文件时发生.
确保serverRelativeUrl正确指定了文件的参数,使用以下格式:
/<web>/<list>/<folder>/<file>
Run Code Online (Sandbox Code Playgroud)
另一个解决方法(我也很惊讶地发现)是删除/主角。
http://site url/_api/web/GetFolderByServerRelativeUrl('/Shared Documents')
Run Code Online (Sandbox Code Playgroud)
http://site url/_api/web/GetFolderByServerRelativeUrl('Shared Documents')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4644 次 |
| 最近记录: |