プラグイン間のデータの移動

 

公開日: 2017年1月

対象: Dynamics 365 (online)、Dynamics 365 (on-premises)、Dynamics CRM 2016、Dynamics CRM Online

Microsoft Dynamics 365 のメッセージ パイプライン モデルでは、実行コンテキストでカスタム データ値のパラメーターのコレクションを定義します。実行コンテキストはパイプラインを介して渡され、登録プラグイン間で共有されます。これは、サードパーティの開発者であっても可能です。 このデータのコレクションをさまざまなプラグインで使用することにより、プラグイン間で情報をやり取りしたり、あるプラグインで処理されたデータを次のプラグインで順次処理することが可能になるチェーン プロセスを有効にすることができます。 この機能は、複数の価格設定プラグイン間でデータをやり取りし、受注または請求書の合計価格を計算する価格設定エンジン シナリオで特に役立ちます。 また、この機能は、プレ イベントに登録されたプラグインとポスト イベントに登録されたプラグイン間で情報をやり取りする場合にも使用できます。

プラグイン間で情報を渡す際に使用されるパラメーターの名前は SharedVariables です。 これは、キー\値のペアです。 プラグインでは、実行時に、SharedVariables コレクションへのプロパティの追加、読み取り、または変更を行うことができます。 この機能によって、プラグイン間で情報をやり取りすることができます。

このサンプルは、SharedVariables を使用して、イベント前に登録されたプラグインからイベント後に登録されたプラグインにデータを渡す方法を示しています。


using System;

// Microsoft Dynamics CRM namespace(s)
using Microsoft.Xrm.Sdk;

namespace Microsoft.Crm.Sdk.Samples
{
    /// <summary>
    /// A plug-in that sends data to another plug-in through the SharedVariables
    /// property of IPluginExecutionContext.
    /// </summary>
    /// <remarks>Register the PreEventPlugin for a pre-operation stage and the 
    /// PostEventPlugin plug-in on a post-operation stage.
    /// </remarks>
    public class PreEventPlugin : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            // Obtain the execution context from the service provider.
            Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
                serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));

            // Create or retrieve some data that will be needed by the post event
            // plug-in. You could run a query, create an entity, or perform a calculation.
            //In this sample, the data to be passed to the post plug-in is
            // represented by a GUID.
            Guid contact = new Guid("{74882D5C-381A-4863-A5B9-B8604615C2D0}");

            // Pass the data to the post event plug-in in an execution context shared
            // variable named PrimaryContact.
            context.SharedVariables.Add("PrimaryContact", (Object)contact.ToString());
        }
    }

    public class PostEventPlugin : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            // Obtain the execution context from the service provider.
            Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
                serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));

            // Obtain the contact from the execution context shared variables.
            if (context.SharedVariables.Contains("PrimaryContact"))
            {
                Guid contact =
                    new Guid((string)context.SharedVariables["PrimaryContact"]);

                // Do something with the contact.
            }
        }
    }
}


' Microsoft Dynamics CRM namespace(s)
Imports Microsoft.Xrm.Sdk

Namespace Microsoft.Crm.Sdk.Samples

    ''' <summary>
    ''' A plug-in that sends data to another plug-in through the SharedVariables
    ''' property of IPluginExecutionContext.
    ''' </summary>
    ''' <remarks>Register the PreEventPlugin for a pre-operation stage and the 
    ''' PostEventPlugin plug-in on a post-operation stage.
    ''' </remarks>
    Public Class PreEventPlugin
        Implements IPlugin

        Public Sub Execute(ByVal serviceProvider As IServiceProvider) _
            Implements IPlugin.Execute

            ' Obtain the execution context from the service provider.
            Dim context As Microsoft.Xrm.Sdk.IPluginExecutionContext =
                CType(serviceProvider.GetService(
                        GetType(Microsoft.Xrm.Sdk.IPluginExecutionContext)), 
                    Microsoft.Xrm.Sdk.IPluginExecutionContext)

            ' Create or retrieve some data that will be needed by the post event
            ' plug-in. You could run a query, create an entity, or perform a calculation.
            'In this sample, the data to be passed to the post plug-in is
            ' represented by a GUID.
            Dim contact As New Guid("{74882D5C-381A-4863-A5B9-B8604615C2D0}")

            ' Pass the data to the post event plug-in in an execution context shared
            ' variable named PrimaryContact.
            context.SharedVariables.Add("PrimaryContact", CType(contact.ToString(),
                                        Object))

        End Sub
    End Class

    Public Class PostEventPlugin
        Implements IPlugin

        Public Sub Execute(ByVal serviceProvider As IServiceProvider) _
            Implements IPlugin.Execute

            ' Obtain the execution context from the service provider.
            Dim context As Microsoft.Xrm.Sdk.IPluginExecutionContext =
                CType(serviceProvider.GetService(
                        GetType(Microsoft.Xrm.Sdk.IPluginExecutionContext)), 
                    Microsoft.Xrm.Sdk.IPluginExecutionContext)

            ' Obtain the contact from the execution context shared variables.
            If context.SharedVariables.Contains("PrimaryContact") Then

                Dim contact As New Guid(CStr(context.SharedVariables("PrimaryContact")))

                ' Do something with the contact.

            End If

        End Sub
    End Class

End Namespace

共有される変数のコレクションには、シリアル化可能な種類のデータを追加することが重要です。そうしないと、データをシリアル化する方法をサーバーが判断できず、プラグインの実行が失敗します。

段階 20 または 40 で登録されたプラグインの場合、作成、更新、削除の際に実行する段階 10 で登録されたプラグインから、または RetrieveExchangeRateRequest によって共有される変数にアクセスするには、ParentContext.SharedVariables コレクションにアクセスする必要があります。 それ以外の場合は、IPluginExecutionContext.SharedVariables にコレクションが格納されます。

関連項目

IPluginExecutionContext
プラグインの開発
プラグインの偽装
イベント実行パイプライン

Microsoft Dynamics 365

© 2017 Microsoft. All rights reserved. 著作権