Wednesday, November 6, 2013

Get data from a remote service using the web proxy in SharePoint 2013

http://code.msdn.microsoft.com/office/SharePoint-2013-Get-data-705bdcd5
http://msdn.microsoft.com/en-us/library/fp179895.aspx
vs2012 -> file -> new -> project c# -> office/SharePoint -> apps -> apps for SharePoint 2013 -> "WebProxyApp"
 

WebProxyApp -> Scripts -> add -> new item -> select JavaScript template and name it as "WebProxyExec.js"
add below custom JavaScript code
"use strict";

(function () {
    var context = SP.ClientContext.get_current();
    var request = new SP.WebRequestInfo();
    request.set_url("http://www.microsoft.com/en-us/news/rss/rssfeed.aspx?ContentType=FeatureStories&Tags=\".NET+Framework\"");
    request.set_method("GET");
    var response = SP.WebProxy.invoke(context, request);

    document.getElementById("stories").innerHTML = "<P>Loading stories...</P>";

    context.executeQueryAsync(successHandler, errorHandler);

    function successHandler() {
        var stories;
        var storiesHTML;
        var xmlDOM;

        if (response.get_statusCode() == 200) {
            xmlDOM = new ActiveXObject("Microsoft.XMLDOM");
            xmlDOM.async = false;
            xmlDOM.loadXML(response.get_body());

            stories = xmlDOM.selectNodes("//title");

            storiesHTML = "<UL>";
            for (var i = 0; i < stories.length; i++) {
                storiesHTML += "<LI>" + stories[i].text + "</LI>";
            }
            storiesHTML += "</UL>";

            document.getElementById("stories").innerHTML = storiesHTML;
        }
        else {
            var errordesc;

            errordesc = "<P>Status code: " + response.get_statusCode() + "<br/>";
            errordesc += response.get_body();
            document.getElementById("stories").innerHTML = errordesc;
        }
    }

    function errorHandler() {
        document.getElementById("stories").innerHTML = response.get_body();
    }
})();

WebProxyApp -> Pages -> Default.aspx
add below code to "PlaceHolderMain" Content PlaceHolder
  <div>
        <p id="message">
            initializing...
        </p>
        <WebPartPages:WebPartZone runat="server" FrameType="TitleBarOnly" ID="full" Title="loc:full" />
        Featured stories in Microsoft News Center:
   
    <span id="stories"></span>
        <script type="text/javascript" src="../_layouts/15/SP.Runtime.js">    </script>
        <script type="text/javascript" src="../_layouts/15/SP.js">    </script>
        <script type="text/javascript" src="../Scripts/WebProxyExec.js">    </script>
 </div>

WebProxyApp -> AppManifest.xml 
1. Go to Genaral Tab
Query String -> Remove this "{StandardTokens}"

2. Go to "Remote EndPoint" Tab on same AppManifest.xml file
Enter this url as EndPoint "http://www.microsoft.com" and Click on 'Add' button

WebProxyApp -> Right click -> Deploy
for this solution only Internet Explorer is working, chrome is not working.


Thank You.

1 comment: