Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
    
Anybody got an idea? –  Pascal Jul 30 '13 at 18:21
add comment

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
    
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 at 11:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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