TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
parthasarathy B
NA
927
108.1k
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.
<script type=
"text/javascript"
>
var hostweburl;
var appweburl;
$(document).ready(function () {
debugger;
//Get the URI decoded URLs.
//hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
//appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl")
hostweburl = _spPageContextInfo.siteAbsoluteUrl;
appweburl = _spPageContextInfo.webAbsoluteUrl;
// 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"
, getListsFromRootSite);
})
function getListsFromRootSite() {
try
{
debugger;
var executor =
new
SP.RequestExecutor(appweburl);
executor.executeAsync(
//jQuery.ajax({
{
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 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 =
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>
Reply
Answers (
2
)
Removing Publishing date on Modern Page
Automatically inserting images using MS-Excel