SharePoint Blog

Consume your custom (SharePoint 2013 hosted) WCF Data Service in a SharePoint 2013 App

I think this will be my shortest post ever. To consume your custom (SharePoint 2013 hosted) WCF Data Service in a SharePoint 2013 App you can simply follow the instructions you find here: http://msdn.microsoft.com/en-us/library/jj163088.aspx.

Then why is this still worth a post? Simply because I think that it’s very easy to oversee this option and start digging in the wrong direction. In my case I believed that because a SharePoint hosted WCF Data Service is basically available at

hostweburl + '/_vti_bin/your_data_service_folder/your_data_service.svc'

It would be also accessible. But I found that available isn’t the equivalent of accessible. I’m still not sure whether I’m facing a flaw in my SharePoint 2013 environment or whether it really is not possible to go beyond the boundaries of the host web using SP.RequestExecutor or AppContextSite (also see my question here: http://social.msdn.microsoft.com/Forums/en-US/appsforsharepoint/thread/92242740-76bc-4f43-97e7-9cba348d2770/#f58df6e4-f73d-400c-8750-0dc05a3f6f7b). But using External ContentTypes for Apps I found a quick and easy way to consume my custom (SharePoint 2013 hosted) WCF Data Service afterall.

One remark

To make things work for a WCF Data Service you may need to change the following line in the sample code

"accept": "application/json",

to

"accept": "application/json; odata=verbose",
  • Tudor Iliescu

    Hi,

    I’ve been searching for a way to call a custom SharePoint service from a SP-Hoste app for days. Can you attach a sample code?

    • http://www.sharepointconsultant.ch/ Marco van Wieren

      Hi Tudor … I guess you would have two options:

      1. Consume your custom service using an external ContentType. I wrote a detailed instruction on how to get this working (incl. Secure Store for keeping credentials if passthrough isn’t working in your case): http://www.sharepointconsultant.ch/2013/11/09/consuming-an-external-odata-service-from-an-sharepoint-2013-app/

      2. If the service is anonymous you maybe could try and use the SP.RequestExecutor for which I included an example below for calling a public Rss service (Don’t forget to add the domain the service is installed in as an allowed remote endpoint to the App’s manifest). But I haven’t tested this … It’s just a thought :)

      // Get current SharePoint context
      var context = SP.ClientContext.get_current();

      // Since we want to make a cross-domain call we need to invoke our request via the SP.WebProxy
      var request = new SP.WebRequestInfo();
      request.set_url(rssEndPoint);
      request.set_method(“GET”);
      var response = SP.WebProxy.invoke(context, request);
      context.executeQueryAsync(
      function () {
      console.log(response.get_body());
      // On return bind the RSS-Feed to our RSS-Template
      new RssFormatter(response.get_body(), koTemplate);
      },

      function () {
      console.log(“ExecuteQueryAsyncException@Rss.Reader”);
      });

Copyright ©2012. All Rights Reserved.