次の方法で共有


カスタム外部アプリケーション プロバイダーを作成する

このトピックでは、カスタム外部アプリケーション プロバイダー (EAP) を作成するプロセスの概要について説明します。

最終更新日: 2010年4月8日

適用対象: SharePoint Foundation 2010

カスタム EAP の作成では、次の 2 つの抽象クラスのどちらかまたは両方からの新しい派生クラスが必要になります。

  • SPExternalApplicationRequestResultSPExternalApplicationRequestResult の派生クラスの実装は、次の 2 つのシナリオで発生します。

    • 外部アプリケーションがインストールされているサイトと要求フォワーダーで、Microsoft SharePoint Foundation から受信するすべての結果に、クライアント ハッシュが含まれている必要がある場合。要求フォワーダーは、このクライアント ハッシュによって、その結果が改ざんされていないことを検証できます。

    • SilverlightWebPart または SharePoint 以外のアプリケーションをホストする Web パーツのレンダリング方法を変更する場合。たとえば、Web パーツの周りにカスタム クロムをレンダリングする場合。

  • SPExternalApplicationProvider – 次のいずれかのシナリオでは、このクラスを実装する必要があります。

    • SPExternalApplicationRequestResult クラス (上記を参照) の派生クラスを実装する場合。

    • Web パーツの外部アプリケーション XML に、読み取りおよび処理されるカスタム マークアップが含まれる場合。

    • Web パーツの子コントロールの作成時に、特殊なロジックを実行する場合。

    • SilverlightToolPart の UI をカスタマイズしたり、ツール パーツの [構成] ボタンをクリックしたときに開くアプリケーション登録ページを変更したりする場合。

重要重要

カスタム EAP を作成するかどうかを検討するときは、指定された Web サービスの子であるすべての SharePoint Foundation Web アプリケーション内のすべてのアプリケーション ホスティング Web パーツで使用できる EAP は 1 つだけであることに注意してください。

要求結果クラスを実装するには

  1. Microsoft Visual Studio 2010 で、空の SharePoint プロジェクトを開始します。

  2. クラス アイテムをプロジェクトに追加します。

  3. クラスを SPExternalApplicationRequestResult からの継承に設定します。

  4. クライアント ハッシュが不要の場合は、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
    
  5. クライアント ハッシュを使用している場合、SPExternalApplicationRequestProperties 型の第 1 パラメーターと、バイト配列 (クライアント ソルトを表します) の第 2 パラメーターを受け取るコンストラクターを作成します。このコンストラクターは、SPExternalApplicationRequestProperties オブジェクトの RequestTokenPrefix プロパティと、クライアント ハッシュ値作成時の入力としてバイト配列を使用します。バイト配列は、外部アプリケーションをホストするサーバーが提供する値である必要があります。またアルゴリズムは、外部アプリケーションがクライアント ハッシュのコピーの作成に使用するアルゴリズムと同じものである必要があります。HashAlgorithmSHA512Managed などの 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 バッキング フィールドに直ちに書き込みを行います。

  6. Silverlight Web パーツのレンダリング、または、SharePoint 以外のアプリケーションをホストするカスタム Web パーツのレンダリングをカスタマイズしない場合は、GetContentControl(String) メソッドを実装して null を返します。しかし、Web パーツのレンダリングをカスタマイズする場合は、このメソッドを実装して、Web パーツの単独の子コントロールである Control を返します。通常、これは HTMK マークアップを含む Literal です。

EAP クラスを実装するには

  1. クラス アイテムをプロジェクトに追加します。

  2. クラスを SPExternalApplicationProvider からの継承に設定します。

  3. 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
    
  4. SPExternalApplicationRequestResult からクラスを派生しない場合、OnApplicationWebPartCreateChildControls(SPExternalApplicationRequestProperties) を実装して null を返すことができます。クラスを派生する場合、このメソッドに実体のある実装を行い、要求結果クラスが構築および使用されるようにすることができます。

    OnApplicationWebPartCreateChildControls(SPExternalApplicationRequestProperties) メソッドは、Web パーツの CreateChildControls メソッドによって呼び出されます。CreateChildControls メソッドは、具体的には次の操作を行います。

    1. Web パーツの登録に使用した外部アプリケーション XML と現在の Web サイトに関する情報から、SPExternalApplicationRequestProperties オブジェクトを作成します。

    2. SPExternalApplicationRequestProperties オブジェクトを OnApplicationWebPartCreateChildControls(SPExternalApplicationRequestProperties) メソッドに渡します。

    3. OnApplicationWebPartCreateChildControls(SPExternalApplicationRequestProperties) メソッドによって返される SPExternalApplicationRequestResult の派生オブジェクトを受け取ります。

    4. 要求結果オブジェクトの GetContentControl(String) メソッドを使用して、Web パーツの単独の子コントロールを取得します。

    図 1: CreateChildControls メソッドによる呼び出し

    Silverlight Web パーツ CreateChildControls

    何らかの理由により、OnApplicationWebPartCreateChildControls(SPExternalApplicationRequestProperties) メソッドが、SharePoint Foundation に組み込まれている既定の EAP 同様 null を返した場合、CreateChildControls メソッドは既定の子コントロールをレンダリングします。これは組み込みの SilverlightWebPartCreateChildControls() メソッドの動作です。

    したがって、OnApplicationWebPartCreateChildControls(SPExternalApplicationRequestProperties) メソッドの実装の第 1 のジョブは、SPExternalApplicationRequestResult の派生クラスのコンストラクターを呼び出し、構築されたオブジェクトを返すことです。次に、OnApplicationWebPartCreateChildControls(SPExternalApplicationRequestProperties) メソッドを上書きする際の注意点をいくつか示します。

    次の例では、上書きされた 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 プロパティに割り当てる点です。次に、ContosoEAPSPExternalApplicationProvider の派生クラスである例を示します。

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 サービスで特定するには

  1. 次のコードをファイルに追加します。

    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()
    
  2. このファイルを EAPIdentify.ps として保存します。

  3. このスクリプトを Windows PowerShell ウィンドウで実行します。