GoBeavs
GoBeavs

Reputation: 499

Call SPService from outside SharePoint page context

Using the SPServices jQuery library from codeplex or any other javascript based solution is it possible to call a SharePoint 2010 web-service from a standalone HTML page using this library and jQuery? Basically I need to upload a file to a existing Document set but I need to do so from a standalone page. The user will be in a single signon situation and logged in to Dynamics CRM.

SPService at CodePlex

Upvotes: 1

Views: 2246

Answers (2)

Joel
Joel

Reputation: 56

This is an old thread, but after much pain trying this myself, I got around authentication woes by creating an iFrame on a standalone html page which itself loads another html page hosted on the SharePoint site. The page loaded in the iFrame uses postMessage() to send the List data to the parent page. This seems to work fine on Firefox and Chrome too.

In summary:

Step 1: Create a html page (SharepointProxy.html) and put this in a Sharepoint list on the site you want to query:

<!DOCTYPE html>
<html>
    <head>
        <title>Web Proxy IFrame</title>
        <meta charset="windows-1252">
        <meta name="viewport" content="width=device-width">
        <script src="jquery-1.10.2.js"></script>
        <script src="jquery.SPServices-2014.01.js"></script>
        <script>
            function callback(e){

                if(e.origin == "https://your.otherdomain.com/index"){ //this is your standalone web page
                    e.source.postMessage(jsonToSend, "https://your.otherdomain.com/index"); //same standalone web page here
                }
                return true;
            }
        </script>
    </head>
    <body>
        <h1>SharePoint proxy - Do Not delete!</h1>
        <p>If you'd like to know further detail about its purpose, please email [email protected]</p>
        <h2>Purpose</h2>
        <p>This page serves as a proxy to call within an Iframe on an external site. This page fetches [whatever]
            from the SharePoint Portal and makes them available as a JSON string</p>


        <script>
            var someListData;
            $().SPServices({
                operation: "GetListItems",
                webURL: "https://sharepoint-portal.com/sites/your_site",
                listName: "List Name",
                async: false,
                completefunc: function(xData, Status) {
                    //alert(xData.responseText);
                    someListData = $(xData.responseXML).find("z\\:row, row").map(function() {
                        return {
                            value: $(this).attr("ows_LinkTitle") || " ",
                            desc: $(this).attr("ows_Details") || " "
                        };
                    }).get();
                }
            });

            var jsonToSend = JSON.stringify(someListData);

            document.addEventListener("message", callback,false);
            window.top.postMessage(jsonToSend, "*");
        </script>
    </body>
</html>

Step 2: On the web page you where you want to display your Sharepoint List data, put these functions to create an iFrame and load your page from SharePoint:

(function() { //create an iFrame to load our SharepointProxy.html page inside of
    var iFrame = document.createElement("iframe");
    iFrame.style.display = "none";
    iFrame.id = "sharePointProxyFrameContainer";
    iFrame.src = "https://sharepoint-portal.com/sites/your_site/Site%20Assets/SharepointProxy.html";
    document.body.appendChild(iFrame);
})();

function processSharePointListData(d){
    var data = JSON.parse(d);
    // do something with data
}

window.addEventListener("message", function(e) {
    if (e.origin === "https://sharepoint-portal.com/") {
        processSharePointListData(e.data);
        return true;
    }

}, false);

Upvotes: 1

Rob Windsor
Rob Windsor

Reputation: 6859

Google "spservices outside sharepoint".

Second link is Must the page using SPServices be hosted within SharePoint?

From the SPServices author:

While the pages where you use SPServices don't have to be within SharePoint, it's common to run into authentication issues if they are not. Either SharePoint doesn't know the user's identity or there can be cross-domain scripting issues. There are far too many variations in all of this for me to usually give a yes or no answer.

Upvotes: 1

Related Questions