I have a web page hosted on a server, say at http://SVR1/path/index.html
, and I would like to access some list items in a on-premises SharePoint site hosted on another server, say at http://SVR2/sites/mySite/
.
The current installation of SharePoint I am using (not under my control) does not allow deployment of neither SharePoint-hosted nor Provider-hosted apps, so I am trying to use the SharePoint Cross-Domain library to access the desired list items from a purely external HTML5/JS/CSS3 page. I, as a user, have full access rights to the list in my SharePoint site, so I guess it should be no problem reading its items.
Following an example found here, my page is as follows:
<!doctype html>
<html>
<head>
<!-- Title -->
<title>Application Template</title>
<script language="javascript" type="text/javascript" src="js/jquery.js"></script>
<script language="javascript" type="text/javascript">
var hostweburl = "http://SVR2/sites/mySite";
var appweburl = "http://SVR1/path";
// Load the required SharePoint libraries
$(document).ready(function () {
$("#renderList").html("Requesting Lists...");
// 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", execCrossDomainRequest);
});
///////////////////////////////////////////////////////
// Function to prepare and issue the request to get
// SharePoint data
function execCrossDomainRequest() {
// executor: The RequestExecutor object
// Initialize the RequestExecutor with the app web URL.
var executor = new SP.RequestExecutor(appweburl);
// Issue the call against the host web.
// To get the title using REST we can hit the endpoint:
// hostweburl/_api/web/lists/getbytitle('listname')/items
// The response formats the data in the JSON format.
// The functions successHandler and errorHandler attend the
// sucess and error events respectively.
executor.executeAsync(
{
url: hostweburl + "/_api/web/lists",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: successHandler,
error: errorHandler
}
);
}
///////////////////////////////////////////////////////
// Function to handle the success event.
// Prints the data to the page.
function successHandler(data) {
var jsonObject = JSON.parse(data.body);
var listsHTML = "";
var results = jsonObject.d.results;
for (var i = 0; i < results.length; i++) {
listsHTML = listsHTML +
"<p><h1>" + results[i].Title +
"</h1>" + results[i].Body +
"</p><hr>";
}
document.getElementById("renderList").innerHTML =
listsHTML;
}
///////////////////////////////////////////////////////
// Function to handle the error event.
// Prints the error message to the page.
function errorHandler(data, errorCode, errorMessage) {
document.getElementById("renderList").innerText =
"Could not complete cross-domain call: " + errorMessage;
}
</script>
</head>
<body>
<h1 id="Root Page" style="text-align:center;">This is the home page</h1>
<div id="renderList">Placeholder</div>
</body>
</html>
When I load the page in a browser, in the javascript console I get an error: "Uncaught Error: Invalid field or parameter requestInfo.url.".
I am under the impression that the problem is in the content of variable appweburl
that, in all the examples I have found, is provided by SharePoint as part of the query part in the URL. But this implies that a provider-hosted app has been deployed in SharePoint - something I cannot do - and that this app is calling its remotely hosted counterpart.
So the question is: Is it possible to use SharePoint cross-domain library in a page totally external to SharePoint, and if yes, how should I set hostweburl
, appweburl
and maybe other things to have access to SharePoint lists?
Thanks in advance.
The two URLs you mentioned, appwebUrl and hostwebUrl are indeed designed for use in apps, so I don't think you should use the cross domain library.
What if you just connect using the non-app way? You could for instance call a custom web service (out of sharepoint), which could connnect through CSOM to your SP site, or you could do this directly in javascript.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With