Daten zwischen Plug-Ins übergeben

 

Veröffentlicht: Januar 2017

Gilt für: Dynamics 365 (online), Dynamics 365 (on-premises), Dynamics CRM 2016, Dynamics CRM Online

Das Messagepipelinemodell für Microsoft Dynamics 365 definiert eine Parametersammlung benutzerdefinierter Datenwerte im Ausführungskontext, der durch die Pipeline übergeben wird und unter registrierten Plug-Ins freigegeben wird, sogar von verschiedenen Drittanbieterentwicklern. Diese Datensammlung kann von verschiedenen Plug-Ins verwendet werden, um Informationen zwischen Plug-Ins zu kommunizieren und Kettenverarbeitung aktivieren, wo die Daten, die von einem Plug-In verarbeitet werden vom nächsten Plug-In in der Reihenfolge verarbeitet werden, usw. Diese Funktion ist Preisberechnungsmodulszenarien besonders nützlich, in denen mehrere Preisberechnungs-Plug-Ins Daten untereinander übergeben, um den Gesamtpreis für einen Vertriebsauftrag oder eine Rechnung zu berechnen. Eine weitere mögliche Verwendung für diese Funktion ist die Kommunikation von Informationen zwischen einem Plug-In, das für ein Pre-Event registriert wurde und einem Plug-In, das für ein Post-Event registriert wurde.

Der Name des Parameters, der zum Übergeben von Informationen zwischen Plug-Ins verwendet wird, ist SharedVariables. Dies ist eine Sammlung von Schlüssel\Wert-Paaren. Zur Laufzeit können Plug-Ins Eigenschaften in der Sammlung SharedVariables hinzufügen, lesen oder ändern. Das bietet eine Möglichkeit zu Informationskommunikation unter Plug-Ins.

Dieses Beispiel zeigt, wie SharedVariables verwendet werden, um Daten aus Pre-Event registrierten Plug-In zu einem Post-Event registrierten Plug-In zu übergeben.


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

Es ist wichtig, dass jeder Datentyp, der der freigegebenen Variablensammlung hinzugefügt wird, serialisierbar ist, andernfalls kann der Server nicht wissen, wie die Daten serialisiert werden und die Plug-In-Ausführung verursacht einen Fehler.

Bei einem Plug-In, das in Phase 20 oder 40 registriert wird, um auf die freigegebenen Variablen eines in Phase 10 registrierten Plug-Ins zuzugreifen, das beim Erstellen, Aktualisieren Löschen oder RetrieveExchangeRateRequest ausgeführt wird, müssen Sie auf die Sammlung ParentContext.SharedVariables zugreifen In allen anderen Fällen enthält IPluginExecutionContext.SharedVariables die Sammlung.

Siehe auch

IPluginExecutionContext
Plug-In-Entwicklung
Identitätswechsel in Plug-Ins
Ereignisausführungspipeline

Microsoft Dynamics 365

© 2017 Microsoft. Alle Rechte vorbehalten. Copyright