Sharepoint Foundation 2010サイトのカスタムWebパーツからロードされたjavascriptからAJAXによって呼び出すことができるWCFを設定します。 Javascript側の処理を簡略化するため、Jsonに呼び出し元に戻すRestfulサービスを提示したいと思います。AJAXを使用してSharepoint WCFにアクセスするときのSPContext.Current null
問題は、AJAX呼び出しでサーバーを呼び出すと、SPContext.Currentがnullであるという問題です。
私はWebサービスを作成するには、SVCファイルに
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$"%>
<%@ServiceHost Language="C#" Debug="true"
Service="Driftportalen.LvService.SuggestService"
Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressWebServiceHostFactory, Microsoft.SharePoint.Client.ServerRuntime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
%>
をMultipleBaseAddressWebServiceHostFactoryを使用していますWebサービスのための契約は、次のようにWebサービスの
[ServiceContract(Namespace = "", ProtectionLevel= ProtectionLevel.None)]
public interface ISuggestServiceTest
{
[WebGet(UriTemplate = "/SuggestAddress/{streetprefix}/", ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
Dictionary<string, GenericAddress> SuggestAddress(string streetprefix);
}
実装は基本的です。
[Guid("BA6733B3-F98D-4AD8-837D-7673F8BC527F")]
[BasicHttpBindingServiceMetadataExchangeEndpoint]
[ServiceBehavior(IncludeExceptionDetailInFaults = true, AddressFilterMode = AddressFilterMode.Any)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class SuggestService : ISuggestServiceTest
{
private SPWeb currentWeb;
public SPWeb CurrentWeb
{
get
{
if (currentWeb == null)
{
var siteUrl = SPContext.Current.Web.Url;
SPSecurity.RunWithElevatedPrivileges(delegate
{
using (var site = new SPSite(siteUrl))
using (var web = site.OpenWeb())
{
currentWeb = web;
}
});
}
return currentWeb;
}
}
public Dictionary<string, GenericAddress> SuggestAddress(string streetprefix)
{
LvService lvService = new LvService(CurrentWeb);
Dictionary<string, GenericAddress> suggestions = new Dictionary<string, GenericAddress>();
//SNIP
//Code that uses lvService to populate suggestions
return suggestions;
}
}
私は、ウェブブラウザからWebサービスを呼び出す場合、すべてが期待どおりに動作し、私は右のデータを取り戻すことをことを確認しました。私はFirebugのを使用して、次のAjax呼び出し
$.ajax({
url: addressUrl + "/"+request.term,
dataType: 'json',
success: function (data) {
responseCallback(data);
$(this).removeClass("fetching");
}
});
を使用
私は正しいURLはJavaScriptから呼び出されたことを確認したと私は、右のコードが実際に到達したことをサーバー側で確認しましたが、SPContext。現在はnullです。
Sharepointサーバーは、Windowsとクレームを使用してログインします。つまり、実際のWCFはSharepointソリューションとは別のアカウントを使用して実行されますが、vti_bin以下のフォルダに展開するので、SharepointはそのコンテキストをWCFに提供する必要があります。私は、AJAXコールがSharepointにそのコンテキストを提供するようには誘発されないと思われますが、ある意味ではそれは匿名です。
は、最初に私は、Webサービス自体はブラウザ専用から呼び出されたときに、それがランダムに失敗するので、非難したと仮定が、私は私が作ることができますどのようにのSharePoint Foundationへのアップグレードをインストールすることで、2010年
をその問題を整理だと思いますSharepointサイトにサインインしたユーザーのコンテキストにWebサービスがアクセスできるようにするJavascriptからのAJAX呼び出しを受け入れるjavascript/WebサービスからのAJAX呼び出し?
これは私の一日を保存しました!あなたはロック:) –