Quantcast
Channel: Developing Apps for SharePoint 2013 forum
Viewing all articles
Browse latest Browse all 8089

HTTP 400 Bad Request when using getfilebyserverrelativeurl or getfolderbyserverrelativeurl

$
0
0

I have been trying to setup a basic CRUD operations sample SharePoint 2013 App. I have a developer template site setup (e.g.
reporting.domain.com/sites/dev). My app has the "web" permission set to "full control".

Inside the host site I have a "Document Library" app/list setup with a file. I am trying to make  a copy of this file using the REST API. Instead I get a 400 "Bad Request" the detailed message says "Value does not fall within the expected range" (below). I am totally stuck. Does anyone know how to resolve this error?


Here is the javascript code I am using.

(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);


    });
}());



Viewing all articles
Browse latest Browse all 8089

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>