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.
- <script type="text/javascript">
- var hostweburl;
- var appweburl;
- $(document).ready(function () {
- debugger;
-
-
-
- hostweburl = _spPageContextInfo.siteAbsoluteUrl;
- appweburl = _spPageContextInfo.webAbsoluteUrl;
-
-
- var scriptbase = hostweburl + "/_layouts/15/";
-
- $.getScript(scriptbase + "SP.RequestExecutor.js", getListsFromRootSite);
-
-
- })
-
-
- function getListsFromRootSite() {
- try {
- debugger;
- var executor = new SP.RequestExecutor(appweburl);
- executor.executeAsync(
-
- {
- url: appweburl + "/_api/SP.AppContextSite('" + hostweburl + "')/Web/lists?$filter=BaseTemplate eq 101&$expand=RootFolder",
- method: "GET",
- headers: {
- "Accept": "application/json; odata=verbose"
- },
- async: false,
- success: function (data) {
- debugger;
- var lists = data.d.results;
- var tempValue = "<span style='text-align:center;display:block;font-size:16px;color:#000;font-weight:bold;'>General Folders</span><br/><br/>";
- tempValue += "<table id='tabledocuments' cellpadding='5' cellspacing='5' style='background-color:#f4f4f4'>";
- lists.forEach(function (list) {
- var itemCount = getDocumentLibrariesItemsCount(_spPageContextInfo.webAbsoluteUrl, list.Title);
- if (!window.location.origin) {
- window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port : '');
- }
- var libraryURL = window.location.origin + list.RootFolder.ServerRelativeUrl;
- console.log("Document URL= " + libraryURL);
- tempValue += "<tr><td><a style='color:#000;text-decoration:none' href='" + libraryURL + "'>" + list.Title + " (" + itemCount + ")</a></td></tr>";
- });
- tempValue += "</table>";
- $("#divRootSiteLibraries").html(tempValue);
- getSubsites();
- },
- error: function (data) {
- console.log(data);
- }
- });
-
- } catch (e) {
- console.log(e);
- }
- }
-
- function getDocumentLibrariesItemsCount(webURL, listTitle) {
- var itemCount;
- try {
- jQuery.ajax({
- url: webURL + "/_api/SP.AppContextSite(@target)/Web/lists/getByTitle('" + listTitle + "')?$select=ItemCount",
- method: "GET",
- headers: {
- "Accept": "application/json; odata=verbose"
- },
- async: false,
- success: function (data) {
- itemCount = data.d.ItemCount;
- },
- error: function (data) {
- console.log(data);
- }
- });
- return itemCount;
- }
- catch (e) {
- console.log(e);
- }
- }
-
- function getDocumentLibrariesCount(webURL) {
- var libraryCount;
- try {
- jQuery.ajax({
- url: webURL + "/_api/SP.AppContextSite(@target)/Web/lists?$filter=BaseTemplate eq 101",
- method: "GET",
- headers: {
- "Accept": "application/json; odata=verbose"
- },
- async: false,
- success: function (data) {
- debugger
- libraryCount = data.d.results.length;
- },
- error: function (data) {
- console.log(data);
- }
- });
- return libraryCount;
- }
- catch (e) {
- console.log(e);
- }
- }
-
- function getSubsites() {
- try {
- jQuery.ajax({
- url: appweburl + "/_api/SP.AppContextSite(@target)/Web/GetSubwebsFilteredForCurrentUser(nWebTemplateFilter=-1)?$filter=WebTemplate ne 'APP'",
- method: "GET",
- headers: {
- "Accept": "application/json; odata=verbose"
- },
- async: false,
- success: function (data) {
- var icount = 1;
- var subSites = data.d.results;
- console.log(subSites);
- var tempValue = "<span style='text-align:center;display:block;font-size:14px;color:#000;font-weight:bold;'>Management Lines</span><br/><br/>";
- tempValue += "<table style='display:inline-block;border:none;' cellpadding='5' cellspacing='5' style='background-color:#f4f4f4'>";
- subSites.forEach(function (site) {
- debugger;
- if (!window.location.origin) {
- window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port : '');
- }
- var webURL = window.location.origin + site.ServerRelativeUrl;
- var librariesCount = getDocumentLibrariesCount(webURL);
- if (icount == 1)
- tempValue += "<tr>";
- tempValue += "<td><table id='tableSubsites' cellpadding='5' cellspacing='5' style='margin:20px;margin-top:0px;text-align:center;display:inline-block'>" +
- "<tr style='font-weight:bold;background-color:#ffffff;'><td style='padding-left:20px;padding-right:20px;'>" + site.Title + "</td></tr>";
- 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>";
- var remainder = icount % 4;
- if (remainder == 0)
- tempValue += "</tr>";
- icount++;
- });
- tempValue += "</table>";
- $("#divSubSites").html(tempValue);
- },
- error: function (data) {
- console.log(data);
- }
- });
- } catch (e) {
- console.log(e);
- }
- }
-
-
-
-
- function getQueryStringParameter(paramToRetrieve) {
- var params =
- document.URL.split("?")[1].split("&");
- var strParams = "";
- for (var i = 0; i < params.length; i = i + 1) {
- var singleParam = params[i].split("=");
- if (singleParam[0] == paramToRetrieve)
- return singleParam[1];
- }
- }
- </script>