Domanda

Ho provato da pochi giorni per chiamare un servizio WCF su misura da un'app di SharePoint Hosted. Ho letto tutte le informazioni disponibili sui forum e non ho trovato una soluzione funzionante.

Quando chiamo il mio servizio WCF personalizzato da un'app console di test, funziona perfettamente. Anche se, quando lo chiamo con un'app di SharePoint Hosted, ottengo un errore "403 proibito". Ho letto che ha a che fare con il fatto che l'AppWeb e il HostWeb siano in un dominio diverso.

Così ho provato la libreria JS "Domain Domain", ma non ho avuto fortuna per far funzionare questo.

Potrei fare qualcosa di sbagliato, forse alcuni di voi possono aiutare ...

Ecco il codice che ho usato nel mio AP per chiamare il servizio:

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); }
        });
}
.

Anche con la biblioteca cross-domain, ottengo ancora un errore proibito di 403.

Ho anche provato ad aggiungere i diritti di lettura nella mia app al livello di raccolta del sito.

Ho modificato l'appmanifest.xml per aggiungere un principal di app (che ho trovato su msdn http://msdn.microsoft.com/en-us/library/fp179927.aspx )

<AppPrincipal>
    <Internal AllowedRemoteHostUrl="~remoteAppUrl" />
</AppPrincipal>
.

Tutto ciò non ha funzionato finora. Qualsiasi aiuto sarà accolto con favore.

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top