使用getfilebyserverrelativeurl或getfolderbyserverrelativeurl时的HTTP 400 Bad Request

时间:2014-10-03 16:21:37

标签: javascript rest sharepoint sharepoint-2013

我一直在尝试设置基本的CRUD操作示例SharePoint 2013 App。

我有一个开发人员模板网站设置(例如reporting.domain.com/sites/dev)。在该网站内,我有一个"文档库" app / list设置文件。我给了" web"在清单中写入权限。

我需要的是通过REST API执行基本的CRUD操作。现在我只是尝试获取和删除操作。我得到了400" Bad Request"背部。我完全陷入困境。想法?

<?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>

这是我正在使用的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);


    });
}());

2 个答案:

答案 0 :(得分:6)

端点http://<sitecollection>/<site>/_api/web/getFileByServerRelativeUrl(serverRelativeUrl)的错误:

Value does not fall within the expected range.
无法找到serverRelativeUrl参数指定的文件时发生

确保正确指定文件的serverRelativeUrl参数,使用以下格式:

/<web>/<list>/<folder>/<file>

答案 1 :(得分:6)

另一种解决方法(我也很惊讶地发现)是删除前导/字符

不工作

http://site url/_api/web/GetFolderByServerRelativeUrl('/Shared Documents')

工作

http://site url/_api/web/GetFolderByServerRelativeUrl('Shared Documents')