Condividi tramite


Passare i dati tra i plug-in

 

Data di pubblicazione: novembre 2016

Si applica a: Dynamics CRM 2015

Il modello della pipeline di messaggio per Microsoft Dynamics 365 definisce una raccolta di parametri di valori dei dati personalizzati nel contesto di esecuzione che viene passata attraverso la pipeline e condivisa tra plug-in registrati, anche da sviluppatori di terze parti. Tale raccolta di dati può essere utilizzata da diversi plug-in per comunicare informazioni tra plug-in e abilitare l'elaborazione a catena in cui i dati elaborati da un plug-in possono essere elaborati dal plug-in successivo nella sequenza e così via. Questa funzionalità è particolarmente utile negli scenari relativi al modulo di determinazione dei prezzi, dove più plug-in di determinazione dei prezzi si passano dati per calcolare il prezzo totale per un ordine di vendita o una fattura. Un altro potenziale utilizzo per questa funzionalità consiste nel comunicare informazioni tra un plug-in registrati per un pre-evento e un plug-in registrato per un post-evento.

Il nome del parametro utilizzato per passare le informazioni tra i plug-in è SharedVariables. La presente è una raccolta di coppie chiave\valore. In fase di esecuzione, i plug-in possono aggiungere, leggere o modificare le proprietà nella raccolta SharedVariables. Ciò fornisce un metodo di comunicazione di informazioni tra plug-in.

In questo esempio viene illustrato come utilizzare SharedVariables per passare dati da un plug-in registrato per un pre-evento a un plug-in registrato per un post-evento.


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

È importante che qualsiasi tipo di dati aggiunto alla raccolta di variabili condivise sia serializzabile, in caso contrario il server non saprà come serializzare i dati e l'esecuzione del plug-in avrà esito negativo.

Per un plug-in registrato in fase 20 o 40, per accedere alle variabili condivise da un plug-in registrato in fase 10 che viene eseguito in crea, aggiorna, elimina o da RetrieveExchangeRateRequest, è necessario accedere alla raccolta ParentContext.SharedVariables. Per tutti gli altri casi, IPluginExecutionContext.SharedVariables include la raccolta.

Vedere anche

IPluginExecutionContext
Sviluppo dei plug-in
Rappresentazione nei plug-in
Pipeline di esecuzione evento

© 2017 Microsoft. Tutti i diritti sono riservati. Copyright