7
votes

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.

1
The posts here greggalipeau.com/2013/02/01 may help. Part 2 is most relevant, but I can't get a direct link to it. You have to add a SharePoint component to the project to get an AppWeb url.Foole
Thanks, Foole, for the link. Actually, it seems to confirm that you need an app (a provider hosted app, in this case) deployed in SP to have correct values in appweburl and hostweburl, but unfortunately I cannot do it in our SP installation - this is the weird point to fix!eca
Did you find a solution for your problem ? If yes, can you explain please?AymKdn
Actually, it seems that the only way is having a provider-hosted app in SharePoint, no way if you can't - as in my case. However this is my conclusion of about one year ago, don't know if anything new has emerged since then.eca
eca - why not just use the Sharepoint REST endpoints to achieve this?user22196

1 Answers

1
votes

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.