parthasarathy B

parthasarathy B

  • NA
  • 927
  • 100.8k

bad request error in the following code in REST API

Sep 25 2018 2:35 AM
Hi all,
           I am develping Sharepoint hosted app using REST API.In that i am adding client webpart.I can't access the host web resources.the error is coming as bad request.Please tell me what mistake i am doing in the below code.Thanks in advance.
  1. <script type="text/javascript">    
  2.        var hostweburl;    
  3.        var appweburl;    
  4.        $(document).ready(function () {    
  5.            debugger;    
  6.            //Get the URI decoded URLs.    
  7.            //hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));    
  8.            //appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl")    
  9.            hostweburl = _spPageContextInfo.siteAbsoluteUrl;    
  10.            appweburl = _spPageContextInfo.webAbsoluteUrl;    
  11.            // resources are in URLs in the form:    
  12.            // web_url/_layouts/15/resource    
  13.            var scriptbase = hostweburl + "/_layouts/15/";    
  14.            // Load the js files and continue to the successHandler    
  15.            $.getScript(scriptbase + "SP.RequestExecutor.js", getListsFromRootSite);    
  16.     
  17.     
  18.        })    
  19.     
  20.     
  21.        function getListsFromRootSite() {    
  22.            try {    
  23.                debugger;    
  24.                var executor = new SP.RequestExecutor(appweburl);    
  25.                executor.executeAsync(    
  26.                //jQuery.ajax({    
  27.                {    
  28.                    url: appweburl + "/_api/SP.AppContextSite('" + hostweburl + "')/Web/lists?$filter=BaseTemplate eq 101&$expand=RootFolder",    
  29.                    method: "GET",    
  30.                    headers: {    
  31.                        "Accept""application/json; odata=verbose"    
  32.                    },    
  33.                    async: false,    
  34.                    success: function (data) {    
  35.                        debugger;    
  36.                        var lists = data.d.results;    
  37.                        var tempValue = "<span style='text-align:center;display:block;font-size:16px;color:#000;font-weight:bold;'>General Folders</span><br/><br/>";    
  38.                        tempValue += "<table id='tabledocuments' cellpadding='5' cellspacing='5' style='background-color:#f4f4f4'>";    
  39.                        lists.forEach(function (list) {    
  40.                            var itemCount = getDocumentLibrariesItemsCount(_spPageContextInfo.webAbsoluteUrl, list.Title);    
  41.                            if (!window.location.origin) {    
  42.                                window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port : '');    
  43.                            }    
  44.                            var libraryURL = window.location.origin + list.RootFolder.ServerRelativeUrl;    
  45.                            console.log("Document URL= " + libraryURL);    
  46.                            tempValue += "<tr><td><a style='color:#000;text-decoration:none' href='" + libraryURL + "'>" + list.Title + " (" + itemCount + ")</a></td></tr>";    
  47.                        });    
  48.                        tempValue += "</table>";    
  49.                        $("#divRootSiteLibraries").html(tempValue);    
  50.                        getSubsites();    
  51.                    },    
  52.                    error: function (data) {    
  53.                        console.log(data);    
  54.                    }    
  55.                });    
  56.                //)    
  57.            } catch (e) {    
  58.                console.log(e);    
  59.            }    
  60.        }    
  61.     
  62.        function getDocumentLibrariesItemsCount(webURL, listTitle) {    
  63.            var itemCount;    
  64.            try {    
  65.                jQuery.ajax({    
  66.                    url: webURL + "/_api/SP.AppContextSite(@target)/Web/lists/getByTitle('" + listTitle + "')?$select=ItemCount",    
  67.                    method: "GET",    
  68.                    headers: {    
  69.                        "Accept""application/json; odata=verbose"    
  70.                    },    
  71.                    async: false,    
  72.                    success: function (data) {    
  73.                        itemCount = data.d.ItemCount;    
  74.                    },    
  75.                    error: function (data) {    
  76.                        console.log(data);    
  77.                    }    
  78.                });    
  79.                return itemCount;    
  80.            }    
  81.            catch (e) {    
  82.                console.log(e);    
  83.            }    
  84.        }    
  85.     
  86.        function getDocumentLibrariesCount(webURL) {    
  87.            var libraryCount;    
  88.            try {    
  89.                jQuery.ajax({    
  90.                    url: webURL + "/_api/SP.AppContextSite(@target)/Web/lists?$filter=BaseTemplate eq 101",    
  91.                    method: "GET",    
  92.                    headers: {    
  93.                        "Accept""application/json; odata=verbose"    
  94.                    },    
  95.                    async: false,    
  96.                    success: function (data) {    
  97.                        debugger    
  98.                        libraryCount = data.d.results.length;    
  99.                    },    
  100.                    error: function (data) {    
  101.                        console.log(data);    
  102.                    }    
  103.                });    
  104.                return libraryCount;    
  105.            }    
  106.            catch (e) {    
  107.                console.log(e);    
  108.            }    
  109.        }    
  110.     
  111.        function getSubsites() {    
  112.            try {    
  113.                jQuery.ajax({    
  114.                    url: appweburl + "/_api/SP.AppContextSite(@target)/Web/GetSubwebsFilteredForCurrentUser(nWebTemplateFilter=-1)?$filter=WebTemplate ne 'APP'",    
  115.                    method: "GET",    
  116.                    headers: {    
  117.                        "Accept""application/json; odata=verbose"    
  118.                    },    
  119.                    async: false,    
  120.                    success: function (data) {    
  121.                        var icount = 1;    
  122.                        var subSites = data.d.results;    
  123.                        console.log(subSites);    
  124.                        var tempValue = "<span style='text-align:center;display:block;font-size:14px;color:#000;font-weight:bold;'>Management Lines</span><br/><br/>";    
  125.                        tempValue += "<table style='display:inline-block;border:none;' cellpadding='5' cellspacing='5' style='background-color:#f4f4f4'>";    
  126.                        subSites.forEach(function (site) {    
  127.                            debugger;    
  128.                            if (!window.location.origin) {    
  129.                                window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port : '');    
  130.                            }    
  131.                            var webURL = window.location.origin + site.ServerRelativeUrl;    
  132.                            var librariesCount = getDocumentLibrariesCount(webURL);    
  133.                            if (icount == 1)    
  134.                                tempValue += "<tr>";    
  135.                            tempValue += "<td><table id='tableSubsites' cellpadding='5' cellspacing='5' style='margin:20px;margin-top:0px;text-align:center;display:inline-block'>" +    
  136.                                "<tr style='font-weight:bold;background-color:#ffffff;'><td style='padding-left:20px;padding-right:20px;'>" + site.Title + "</td></tr>";    
  137.                            tempValue += "<tr style='background-color:#f4f4f4;'><td style='padding-left:20px;padding-right:20px;'><a style='color:#000;text-decoration:none;' href='" + webURL + "'>Active Issuers (" + librariesCount + ")</a></td></tr></table></td>";    
  138.                            var remainder = icount % 4;    
  139.                            if (remainder == 0)    
  140.                                tempValue += "</tr>";    
  141.                            icount++;    
  142.                        });    
  143.                        tempValue += "</table>";    
  144.                        $("#divSubSites").html(tempValue);    
  145.                    },    
  146.                    error: function (data) {    
  147.                        console.log(data);    
  148.                    }    
  149.                });    
  150.            } catch (e) {    
  151.                console.log(e);    
  152.            }    
  153.        }    
  154.     
  155.        // Function to retrieve a query string value.    
  156.        // For production purposes you may want to use    
  157.        //  a library to handle the query string.    
  158.        function getQueryStringParameter(paramToRetrieve) {    
  159.            var params =    
  160.                document.URL.split("?")[1].split("&");    
  161.            var strParams = "";    
  162.            for (var i = 0; i < params.length; i = i + 1) {    
  163.                var singleParam = params[i].split("=");    
  164.                if (singleParam[0] == paramToRetrieve)    
  165.                    return singleParam[1];    
  166.            }    
  167.        }    
  168.    </script>    

Answers (2)