カスタム外部アプリケーション プロバイダーを作成する
このトピックでは、カスタム外部アプリケーション プロバイダー (EAP) を作成するプロセスの概要について説明します。
最終更新日: 2010年4月8日
適用対象: SharePoint Foundation 2010
カスタム EAP の作成では、次の 2 つの抽象クラスのどちらかまたは両方からの新しい派生クラスが必要になります。
SPExternalApplicationRequestResult – SPExternalApplicationRequestResult の派生クラスの実装は、次の 2 つのシナリオで発生します。
外部アプリケーションがインストールされているサイトと要求フォワーダーで、Microsoft SharePoint Foundation から受信するすべての結果に、クライアント ハッシュが含まれている必要がある場合。要求フォワーダーは、このクライアント ハッシュによって、その結果が改ざんされていないことを検証できます。
SilverlightWebPart または SharePoint 以外のアプリケーションをホストする Web パーツのレンダリング方法を変更する場合。たとえば、Web パーツの周りにカスタム クロムをレンダリングする場合。
SPExternalApplicationProvider – 次のいずれかのシナリオでは、このクラスを実装する必要があります。
SPExternalApplicationRequestResult クラス (上記を参照) の派生クラスを実装する場合。
Web パーツの外部アプリケーション XML に、読み取りおよび処理されるカスタム マークアップが含まれる場合。
Web パーツの子コントロールの作成時に、特殊なロジックを実行する場合。
SilverlightToolPart の UI をカスタマイズしたり、ツール パーツの [構成] ボタンをクリックしたときに開くアプリケーション登録ページを変更したりする場合。
重要 |
---|
カスタム EAP を作成するかどうかを検討するときは、指定された Web サービスの子であるすべての SharePoint Foundation Web アプリケーション内のすべてのアプリケーション ホスティング Web パーツで使用できる EAP は 1 つだけであることに注意してください。 |
要求結果クラスを実装するには
Microsoft Visual Studio 2010 で、空の SharePoint プロジェクトを開始します。
クラス アイテムをプロジェクトに追加します。
クラスを SPExternalApplicationRequestResult からの継承に設定します。
クライアント ハッシュが不要の場合は、ClientHash プロパティを、null を返すように実装します。外部アプリケーションに返す結果にクライアント ハッシュを含める場合は、ClientHash プロパティを、Byte 配列であるプライベート バッキング フィールドのラッパーとして実装します。
private Byte[] clientHash; public override Byte[] ClientHash { get { return clientHash; } }
Private clientHash_Renamed() As Byte Public Overrides ReadOnly Property ClientHash() As Byte() Get Return clientHash_Renamed End Get End Property
クライアント ハッシュを使用している場合、SPExternalApplicationRequestProperties 型の第 1 パラメーターと、バイト配列 (クライアント ソルトを表します) の第 2 パラメーターを受け取るコンストラクターを作成します。このコンストラクターは、SPExternalApplicationRequestProperties オブジェクトの RequestTokenPrefix プロパティと、クライアント ハッシュ値作成時の入力としてバイト配列を使用します。バイト配列は、外部アプリケーションをホストするサーバーが提供する値である必要があります。またアルゴリズムは、外部アプリケーションがクライアント ハッシュのコピーの作成に使用するアルゴリズムと同じものである必要があります。HashAlgorithm、SHA512Managed などの System.Security.Cryptography 名前空間のクラスを使用することを検討してください。次に例を示します。この例のように SHA512Managed クラスを使用するには、コード ファイルに、System.Security.Cryptography 名前空間に対する using (Visual Basic では Imports) ステートメントを追加する必要があります。
public CustomRequestResult() { } public CustomRequestResult(SPExternalApplicationRequestProperties externalAppRequest, byte[] clientSalt) { string prefix = externalAppRequest.RequestTokenPrefix; int nCount = Encoding.Unicode.GetByteCount(prefix); nCount += clientSalt.Length; byte[] bytes = new byte[nCount]; nCount = Encoding.Unicode.GetBytes(prefix, 0, prefix.Length, bytes, 0); for (int i = 0; i < clientSalt.Length; i++) { bytes[nCount + i] = clientSalt[i]; } // Compute the hash value SHA512Managed sha512 = new SHA512Managed(); clientHash = sha512.ComputeHash(bytes); }
Public Sub New() End Sub Public Sub New(ByVal externalAppRequest As SPExternalApplicationRequestProperties, ByVal clientSalt() As Byte) Dim prefix As String = externalAppRequest.RequestTokenPrefix Dim nCount As Integer = Encoding.Unicode.GetByteCount(prefix) nCount += clientSalt.Length Dim bytes(nCount - 1) As Byte nCount = Encoding.Unicode.GetBytes(prefix, 0, prefix.Length, bytes, 0) For i As Integer = 0 To clientSalt.Length - 1 bytes(nCount + i) = clientSalt(i) Next i ' Compute the hash value Dim sha512 As New SHA512Managed() clientHash = sha512.ComputeHash(bytes) End Sub
派生クラスが使用する SPExternalApplicationRequestProperties クラスの部分のみが RequestTokenPrefix プロパティの場合、コンストラクターは第 1 パラメーターに String オブジェクトを受け取り、RequestTokenPrefix プロパティを渡すコードを呼び出すことができます (このコードは、以下で説明する OnApplicationWebPartCreateChildControls(SPExternalApplicationRequestProperties) メソッドです)。もう 1 つの方法は、派生型のオブジェクトを構築する前にクライアント ハッシュを作成する、呼び出し元のコードを設計することです。その場合、その型のコンストラクターは、ハッシュ値自体をバイト配列パラメーターとして受け取ることができ、clientHash バッキング フィールドに直ちに書き込みを行います。
Silverlight Web パーツのレンダリング、または、SharePoint 以外のアプリケーションをホストするカスタム Web パーツのレンダリングをカスタマイズしない場合は、GetContentControl(String) メソッドを実装して null を返します。しかし、Web パーツのレンダリングをカスタマイズする場合は、このメソッドを実装して、Web パーツの単独の子コントロールである Control を返します。通常、これは HTMK マークアップを含む Literal です。
EAP クラスを実装するには
クラス アイテムをプロジェクトに追加します。
クラスを SPExternalApplicationProvider からの継承に設定します。
SilverlightToolPart の UI をカスタマイズする場合は、GetRegistrationInformation(SPWeb) メソッドを実装します。このメソッドが返す SPExternalApplicationRegistrationInformation オブジェクトの 5 つのプロパティは、いずれも変更できます。たとえば、カスタム登録ページを置き換える場合、カスタム ページの URL を HyperlinkUrl プロパティに割り当てます。インライン コードまたはカスタム ページに含まれるコードを使用して、ユーザーに他のサービスを提供できます。たとえば、コードでアプリケーション XML を読み取り、アプリケーション プリンシパル名が指定されているかどうかを確認できます。アプリケーション プリンシパル名が指定されている場合、コードによってそのアプリケーション プリンシパル ユーザーが存在するかどうかが確認され、存在しない場合は作成されます。
次の例では、ページのボタン名を "Configure" から "Register" に変更し、ボタンの説明テキストと、ボタンのクリックで開くダイアログのサイズをそれぞれ変更しています。また、ボタンのクリックで、カスタムの別のアプリケーション登録ページを開くように指定しています (既定の登録ページは newslwp.aspx です)。
public override SPExternalApplicationRegistrationInformation GetRegistrationInformation(SPWeb web) { SPExternalApplicationRegistrationInformation info = new SPExternalApplicationRegistrationInformation(); info.Description = "To register a Silverlight application (.xap), click Register"; info.DialogHeight = 600; info.DialogWidth = 500; string url = web.ServerRelativeUrl; if (!url.EndsWith("/")) { url = url + "/"; } url += "_layouts/alternateReg.aspx"; info.HyperlinkText = "Register"; info.HyperlinkUrl = url; return info; }
Public Overrides Function GetRegistrationInformation(ByVal web As SPWeb) As SPExternalApplicationRegistrationInformation Dim info As New SPExternalApplicationRegistrationInformation() info.Description = "To register a Silverlight application (.xap), click Register" info.DialogHeight = 600 info.DialogWidth = 500 Dim url As String = web.ServerRelativeUrl If Not url.EndsWith("/") Then url = url & "/" End If url &= "_layouts/alternateReg.aspx" info.HyperlinkText = "Register" info.HyperlinkUrl = url Return info End Function
SPExternalApplicationRequestResult からクラスを派生しない場合、OnApplicationWebPartCreateChildControls(SPExternalApplicationRequestProperties) を実装して null を返すことができます。クラスを派生する場合、このメソッドに実体のある実装を行い、要求結果クラスが構築および使用されるようにすることができます。
OnApplicationWebPartCreateChildControls(SPExternalApplicationRequestProperties) メソッドは、Web パーツの CreateChildControls メソッドによって呼び出されます。CreateChildControls メソッドは、具体的には次の操作を行います。
Web パーツの登録に使用した外部アプリケーション XML と現在の Web サイトに関する情報から、SPExternalApplicationRequestProperties オブジェクトを作成します。
SPExternalApplicationRequestProperties オブジェクトを OnApplicationWebPartCreateChildControls(SPExternalApplicationRequestProperties) メソッドに渡します。
OnApplicationWebPartCreateChildControls(SPExternalApplicationRequestProperties) メソッドによって返される SPExternalApplicationRequestResult の派生オブジェクトを受け取ります。
要求結果オブジェクトの GetContentControl(String) メソッドを使用して、Web パーツの単独の子コントロールを取得します。
図 1: CreateChildControls メソッドによる呼び出し
何らかの理由により、OnApplicationWebPartCreateChildControls(SPExternalApplicationRequestProperties) メソッドが、SharePoint Foundation に組み込まれている既定の EAP 同様 null を返した場合、CreateChildControls メソッドは既定の子コントロールをレンダリングします。これは組み込みの SilverlightWebPart の CreateChildControls() メソッドの動作です。
したがって、OnApplicationWebPartCreateChildControls(SPExternalApplicationRequestProperties) メソッドの実装の第 1 のジョブは、SPExternalApplicationRequestResult の派生クラスのコンストラクターを呼び出し、構築されたオブジェクトを返すことです。次に、OnApplicationWebPartCreateChildControls(SPExternalApplicationRequestProperties) メソッドを上書きする際の注意点をいくつか示します。
SPExternalApplicationRequestResult 派生オブジェクトに null 以外の ClientHash プロパティがある場合、OnApplicationWebPartCreateChildControls(SPExternalApplicationRequestProperties) は、SPExternalApplicationRequestResult の派生クラスに対して作成された、既定以外のコンストラクターを呼び出す必要があります。OnApplicationWebPartCreateChildControls(SPExternalApplicationRequestProperties) は、このコンストラクターに、呼び出し時にパラメーターとして受け取ったものと同じ SPExternalApplicationRequestProperties オブジェクトを渡す必要があります。
要求結果クラスが ClientHash に実体のある実装を行わない場合 (したがって既定のコンストラクターだけがある場合)、Web パーツの CreateChildControls メソッドに SPExternalApplicationRequestResult オブジェクトを指定して GetContentControl(String) メソッドを呼び出すようにできます。SPExternalApplicationRequestResult からクラスを派生する場合、OnApplicationWebPartCreateChildControls(SPExternalApplicationRequestProperties) メソッドを実装する必要があるのはこのためです。
OnApplicationWebPartCreateChildControls(SPExternalApplicationRequestProperties) メソッドの実装によって他の操作も同様に行うことができます。たとえば、外部アプリケーション XML の他のカスタム プロパティを読み取ることもできます。
次の例では、上書きされた OnApplicationWebPartCreateChildControls(SPExternalApplicationRequestProperties) が、CustomRequestResult という名前の SPExternalApplicationRequestResult の派生クラスの、既定以外のコンストラクターを呼び出します。既定以外のコンストラクターの詳細については、前述の「要求結果クラスを実装するには」の手順を参照してください。
public override SPExternalApplicationRequestResult OnApplicationWebPartCreateChildControls( SPExternalApplicationRequestProperties args) { SPExternalApplicationRequestResult reqRes = CustomRequestResult(args, saltFromApplication); return reqRes; }
Public Overrides Function OnApplicationWebPartCreateChildControls(ByVal args As SPExternalApplicationRequestProperties) As SPExternalApplicationRequestResult Dim reqRes As SPExternalApplicationRequestResult = CustomRequestResult(args, saltFromApplication) Return reqRes End Function
EAP を Web サービスで特定する
SharePoint Foundation に対する識別のプロセスでは、カスタム EAP は「[方法] 外部アプリケーション プロバイダーを有効にする」で説明した EAP の有効化と実質的に同じ手順を実行します。唯一の違いは、EAP を有効にする代わりに、EAP 型のオブジェクトを作成し、それを ExternalApplicationSettings.Provider プロパティに割り当てる点です。次に、ContosoEAP が SPExternalApplicationProvider の派生クラスである例を示します。
ContosoEAP exAppProvider = new ContosoEAP();
SPWebService.ContentService.ExternalApplicationSettings.Provider = exAppProvider;
SPWebService.ContentService.ExternalApplicationSettings.Enabled = true;
SPWebService.ContentService.Update();
Dim exAppProvider As New ContosoEAP()
SPWebService.ContentService.ExternalApplicationSettings.Provider = exAppProvider
SPWebService.ContentService.ExternalApplicationSettings.Enabled = True
SPWebService.ContentService.Update()
ヒント |
---|
Web サービスに対して外部アプリケーション管理が既に有効になっている場合、Enabled プロパティを設定する行は必要ありませんが、あっても構いません。外部アプリケーション管理がまだ有効になっていない場合は、その行を含めると、「[方法] 外部アプリケーション プロバイダーを有効にする」での手順が不要になります。ただし、その逆は正しくありません。つまり、既定の EAP を使用する場合でも、管理を有効にするコードを実行する必要があります。 |
前述のコードは、「[方法] 外部アプリケーション プロバイダーを有効にする」で説明したどの方法でも実行できます。次の例は、Windows PowerShell コマンド ライン インターフェイス スクリプトの Windows PowerShell Add-Type コマンドレットでコードを実行する方法を示しています。
EAP を Web サービスで特定するには
次のコードをファイルに追加します。
Add-type @" using System; using Microsoft.SharePoint.Administration; namespace ContosoCmdlets public class EAPIdentifier { public static void IdentifyEAP() { ContosoEAP exAppProvider = new ContosoEAP(); SPWebService.ContentService.ExternalApplicationSettings.Provider = exAppProvider; SPWebService.ContentService.ExternalApplicationSettings.Enabled = true; SPWebService.ContentService.Update(); } } "@ -Language CsharpVersion3 [ContosoCmdlets.EAPIdentifier]::IdentifyEAP()
このファイルを EAPIdentify.ps として保存します。
このスクリプトを Windows PowerShell ウィンドウで実行します。