2

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.

1
  • Anybody got an idea?
    – Pascal
    Jul 30, 2013 at 18:21

1 Answer 1

1

I have found a work around.

I deployed my custom WCF to IIS instead of deploying it into SharePoint (ISAPI). I had to make the WCF service public (to be able to call it cross-domain) by adding this code into the WCF's web.config:

<system.webServer>
    <httpProtocol>
        <customHeaders>
            <add name="Access-Control-Allow-Origin" value="*" />
        </customHeaders>
   </httpProtocol>
</system.webServer>

I then deployed the WCF to IIS and from there I have been able to call if from a standard Jquery Ajax Call:

$(document).ready(function () {

    $.ajax({
        type: "GET",
        url: "http://services.ca/SQLService/DataService.svc/GetData?query=query",
        dataType: 'json',
        processdata: true,
        success: function (msg) {
            var JSONmsg = JSON.stringify(msg);

            var Response = document.getElementById('ServiceResponse');
            Response.innerText = JSONmsg;


        },
        error: function (data, errorCode, errorMessage) {
            alert('Error: ' + errorMessage);
        }
    });
});

Hope this helps.

1
  • Can we write a code to read audit logs in wcf service for SharePoint online? So I want to pass a url of page that user visited to a wcf service and wcf service then gets audit logs based on passed url. Is it possible?
    – mit
    Feb 18, 2014 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.