Partager via


Passer des données entre les plug-ins

 

Date de publication : janvier 2017

S’applique à : Dynamics 365 (online), Dynamics 365 (on-premises), Dynamics CRM 2016, Dynamics CRM Online

Le modèle de pipeline des messages pour Microsoft Dynamics 365 définit une collection de paramètres des valeurs de données personnalisées dans le contexte d’exécution qui est passé via le pipeline et partagé entre les plug-ins inscrits, même de différents développeurs tiers. Cette collection de données peut être utilisée par les plug-ins pour communiquer des informations entre eux et activer le traitement des chaînes où des données traitées par un plug-in peuvent être traitées par le plug-in suivant dans la séquence, et ainsi de suite. Cette fonction est particulièrement utile dans les scénarios de moteur de tarification où plusieurs plug-ins de tarification se passent des données entre eux pour calculer le coût total d’une commande ou d’une facture. Une autre utilisation potentielle de cette fonctionnalité consiste à communiquer des informations entre un plug-in inscrit pour un pré-événementiel et un plug-in inscrit pour un post-événementiel.

Le nom du paramètre utilisé pour passer les informations entre les plug-ins est SharedVariables. Il s’agit d’une collection de paires clé\valeur. Au moment de l’exécution, les plug-ins peuvent ajouter, lire ou modifier les propriétés de la collection SharedVariables. Cela fournit une méthode de communication d’informations entre les plug-ins.

Cet exemple montre comment utiliser SharedVariables pour passer les données d’un plug-in inscrit pour un pré-événementiel à un plug-in inscrit pour un post-événementiel.


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

Il est important que tous les types de données ajoutés à la collection de variables partagées soient sérialisées sinon le serveur ne sait pas comment sérialiser les données et l’exécution du plug-in échoue.

Pour un plug-in inscrit en phase 20 ou 40, afin d’accéder aux variables partagées à partir d’un plug-in inscrit en phase 10 qui s’exécute lors d’une création, mise à jour, suppression ou par RetrieveExchangeRateRequest, vous devez accéder à la collection ParentContext.SharedVariables. Pour tous les autres incidents, IPluginExecutionContext.SharedVariables contient la collection.

Voir aussi

IPluginExecutionContext
Développement de plug-ins
Emprunt d’identité des plug-ins
Pipeline d’exécution des événements

Microsoft Dynamics 365

© 2017 Microsoft. Tous droits réservés. Copyright