SharePointホストアプリでカスタムWCFを使用してください
-
10-12-2019 - |
質問
SharePointホストアプリからカスタムメイドのWCFサービスを呼び出すのに数日以内に試みてきました。私はフォーラムで利用可能な情報全体を読んで、私は作業解を見つけることができませんでした。
テストコンソールアプリからカスタムWCFサービスを呼び出すと、完全に機能します。 SharePointホストアプリケーションと呼びますが、「403禁止」エラーが発生しますが。私はそれを読んで、AppwebとHostwebが別のドメインにあるという事実と関係があります。
だから私は「クロスドメイン」JSライブラリを試しましたが、この作業を手に入れることはできませんでした。
私は何か悪いことをしているかもしれません、たぶんあなたの何人かは助けることができます...
これは私のAPで私がサービスを呼び出すコードです:
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); }
});
}
.
クロスドメインライブラリでさえ、私はまだ403の禁止エラーを受けます。
私はまた、私のアプリの読み取り権限をサイト収集レベルに追加しようとしました。
私はAppManifest.xmlを編集して、アプリプリンシパル(MSDN http://msdn.microsoft.com/en-us/library/fp179927.aspx )
<AppPrincipal>
<Internal AllowedRemoteHostUrl="~remoteAppUrl" />
</AppPrincipal>
.
これまでのところうまくいきませんでした。あらゆる助けが歓迎されます。
解決
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.