I have been trying since a few days to call a custom made WCF service from a SharePoint Hosted App. I have read all over the information available on forums and I couldn't find a working solution.
When I call my custom WCF service from a test console app, it works perfectly. Although, when I call it withing a SharePoint Hosted App, I get a "403 Forbidden" Error. I read it has to do with the fact that the AppWeb and the HostWeb are in a different domain.
So I tried the "cross-domain" JS Library, but I had no luck to get this working.
I might be doing something wrong, maybe some of you can help...
Here is the code I used in my ap to call the Service:
var hostweburl;
var appweburl;
$(document).ready(function () {
hostweburl = decodeURIComponent(getParameterByName('SPHostUrl'));
appweburl = decodeURIComponent(getParameterByName('SPAppWebUrl'))
var scriptbase = hostweburl + "/_layouts/15/";
$.getScript(scriptbase + "SP.RequestExecutor.js", CallService);
});
function CallService()
{
var exec;
exec = new SP.RequestExecutor(appweburl);
exec.executeAsync(
{
url: appweburl + "/_vti_bin/CSN.DataService/DataService.svc/GetData?query=QueryHere",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (msg) {
alert(msg);
},
error: function (data, errorCode, errorMessage) { alert('Error: ' + errorMessage); }
});
}
Even with the Cross-Domain Library, I still get a 403 Forbidden error.
I have also tried to add read rights in my app to the site collection level.
I have edited the AppManifest.xml to add an app principal (which I found on MSDN http://msdn.microsoft.com/en-us/library/fp179927.aspx)
<AppPrincipal>
<Internal AllowedRemoteHostUrl="~remoteAppUrl" />
</AppPrincipal>
All of that didn't work so far. Any help will be welcomed.