2

I am trying to get data from SharePoint and display it in an HTML page on a separate domain using SharePoint 2013's API. The SP instance sits on-prem and the other domain is also within our firewall (on a different server). Here are some of the resources I used to put this together: http://msdn.microsoft.com/en-us/library/office/jj164022(v=office.15).aspx

http://weshackett.com/2014/02/sp-requestexecutor-cross-domain-calls-using-rest-gotcha/

<script src="https://ideas.simplot.com/_layouts/15/SP.RequestExecutor.js"></script>
<script>
        var executor;
        var appWebUrl = "http://mysharepointdomain";
        var hostWebUrl = "http://theotherdomain";
        // Initialize the RequestExecutor with the app web URL.
        executor = new SP.RequestExecutor(appWebUrl);

        function successHandler(data) {
            console.log(data);   
        }
        function errorHandler(data) {
            console.log(data);   
        }

        //Get all the available task lists from the host web
        function getData() {
            executor.executeAsync({
                //url:appWebUrl + "/_api/SP.AppContextSite(@target)/web/lists/?$filter=BaseTemplate eq 171&$select=ID,Title,ImageUrl,ItemCount,ListItemEntityTypeFullName&@target='" + hostWebUrl + "'",
                url:appWebUrl + "/_api/SP.AppContextSite(@target)/web?@target='" + hostWebUrl + "'",
                method: "GET",
                headers: {  "Accept": "application/json; odata=verbose" },
                success: successHandler, 
                error:  errorHandler
            });
        }
        getData();
</script>

This is what I get back from the call in the browsers; which is essentially nothing...

SP.ResponseInfo {responseAvailable: false, body: "", statusCode: undefined, statusText: undefined, contentType: undefined…}
allResponseHeaders: undefined
binaryStringResponseBody: undefined
body: ""
contentType: undefined
headers: null
responseAvailable: false
state: undefined
statusCode: undefined
statusText: undefined

And this is the XML I get if I enter the app web url into the browser.

Example URL: <mysharepointdomain>/_api/SP.AppContextSite%28@target%29/web?@target=%27theotherdomain%27

XML Returned in the browser: 
<m:error>
 <m:code>-1, Microsoft.SharePoint.SPException</m:code>
 <m:message xml:lang="en-US">There is no app context to execute this request.</m:message>
</m:error>

If anyone has any insight on why this is not working, it would be much appreciated. The other domain site is not a Sharepoint app or page. It is a non Sharepoint application sitting on a Linux server. The site is not passing any user credentials to sp right now which may be part of the issue?

5
  • Is this being run on a SharePoint App or SharePoint page? Or is it just as an anonymous user?
    – z.rahui
    Jul 24, 2014 at 1:04
  • 1
    Just to verify, you are running this script on a non-SP page?
    – wjervis
    Jul 24, 2014 at 11:19
  • @wjervis Correct. It is running on a non-SP page.
    – jenslisn
    Jul 24, 2014 at 14:22
  • @z.rahui It is not an SP app or page, so it is an anonymous user.
    – jenslisn
    Jul 24, 2014 at 14:28
  • I am getting same response with all undefined. However, in my case, it is a client part created in sharepoint hosted and then added that app part on a site page. what would be the reason for this? However, i am able to read list items using REST url without any issues. Only for POST request, i am getting this issue.
    – user703326
    Jun 12, 2016 at 20:38

1 Answer 1

1

An Anonymous user won't have a SP/App Context. You can run the code above in a Provider hosted app for an anonymous user but you will need to request an 'App Only' token which means that the context is using the Apps identity not the user (as it's anonymous). You should be able to do the same for a SharePoint hosted app as well.

Remember, to create a ClientContext for say a console app, you would create it using client credentials. Or as JSOM you would use context from the SP page. How can the context be created for anonymous that's not in SP?

There are exceptions to this when we remove the 'Require Use remote interfaces permission' setting: http://blogs.msdn.com/b/kaevans/archive/2013/10/24/what-every-developer-needs-to-know-about-sharepoint-apps-csom-and-anonymous-publishing-sites.aspx

Have you tried using the JSOM/CSOM instead of REST? You should be able to set SP.ClientContext directly and specify your SP url: http://msdn.microsoft.com/en-us/library/office/jj245896(v=office.15).aspx

I'd be interested to know how you get on as I haven't had to use the REST for anonymous users outside of SharePoint before.

For more info on Creating SharePoint Apps for Anonymous users: http://msdn.microsoft.com/en-us/library/office/dn579415(v=office.15).aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.