Accessing data from an external service in a SharePoint App

So you can access remote data from a SharePoint using the Web Proxy, but what if you don’t know the domain of the service you’ll want to access beforehand. That means you can’t add it to the manifest for your App and so the Web Proxy will not work. Another option is JsonP, but not all services provide JsonP support. How to access them? This is my solution to the problem. I am hoping that someone will say “Vince, you’re over complicating it, this is the easy way”, but until that happens this is the solution I am currently using. I looked at using Azure, but the free websites have restrictions and I didn’t want things to stop working if I hit them. So, as much as I’m not a Google fan, Google Scripts it was. Basically I have a script which takes a Url and then returns the result of that Url in JsonP. If you visit Google Scripts then you can create a new script and publish it. This is what my script looks like. And here is the code… function doGet(e) { return ContentService .createTextOutput(urlGet(e)) .setMimeType(ContentService.MimeType.JSON); } function urlGet(e) { var results = {}; if (!e.parameter.url) { results = "{error:'missing url',results: null }"; } else { var response = UrlFetchApp.fetch(e.parameter.url).getContentText(); if (!response) {results = "{error:'no data returned',results: null}";} else {results = response;} } //results = JSON.stringify(results); if (e.parameter.callback) { results = e.parameter.callback + "(" + results + ")" } return results; } .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } This code expects JSON from the remote service, but there is a line to un-comment if you are getting a string or XML. Once you publish this script (ensure that you make it public) then you get a Url to your script which you can use in your App to download any JSON from anywhere. Your JavaScript would be similar to this… var dataUrl = "http://<the URL to the data you want to access>"; var url = "https://script.google.com/macros/s/" + "AK1ycbxFZcqlPNgUIz97Q1FSX-5akze8X3Lx36iNqE_ltmBvfgWjSxc/exec?url=" + encodeURIComponent(dataUrl) + "&_callback=?"; $.ajax({ url: url, crossDomain: true, success: function (o, a, x) { // Do something useful with your data }, error: function (o, a, x) { alert("Failed"); } }); Note: That is not the Url to my service and it will not work…create your own, it’s free and is just a bit of copy ‘n’ paste of the code. I have used this code to get RSS feeds, but you will not get a success, you get a parse failure because it’s not JSON. Fortunately if you change the ‘success’ handler to an ‘error’ handler in the $.Ajax call you will get the response code (200) and the body of the response (containing your XML). A bit of a hack, but it does work. Another option for getting some XML or an RSS feed is to use Yahoo Pipes. These can, amongst numerous other things, turn your XML/RSS into a valid JSON feed. This you can then use as the Url in your SharePoint App to convert the data before passing it to the Goole Script. Lots of indirection, but it does all work and is plenty quick enough.

SharePoint on Windows Phone

AgilisPoint a free App for your Windows Phone