Del via


Overføre data mellem plug-ins

 

Udgivet: januar 2017

Gælder for: Dynamics 365 (online), Dynamics 365 (on-premises), Dynamics CRM 2016, Dynamics CRM Online

Meddelelsespipelinemodellen til Microsoft Dynamics 365 definerer en parameterindsamling af brugerdefinerede værdier i kørselskonteksten, der sendes gennem pipelinen og deles af registrerede plug-ins, endda fra forskellige tredjepartsudviklere. Denne samling af data kan bruges af forskellige plug-ins til at kommunikere oplysninger mellem plug-ins og aktivere kædebehandling, hvor data, der behandles af en plug-in, kan behandles af næste plug-in i rækkefølgen og så videre. Denne funktion er især praktisk i prissætningsscenarier, hvor flere prissætningsplug-ins overfører data mellem hinanden til at beregne den samlede pris for en salgsordre eller en faktura. En anden mulig brug af denne funktion er at kommunikere oplysninger mellem en plug-in, der er registreret for en før-hændelse, og en plug-in, der er registreret for en efterfølgende hændelse.

Navnet på den parameter, der bruges til at overføre oplysninger mellem plug-ins, er SharedVariables. Dette er en samling af key\value-par. På kørselstidspunktet kan plug-ins tilføje, læse eller redigere egenskaber i samlingen SharedVariables. Dette giver en metode til kommunikation af oplysninger mellem plug-ins.

I dette eksempel vises, hvordan du bruger SharedVariables til at overføre data fra en plug-in, der er registreret for en før-hændelse, til en plug-in, der er registreret for en efterfølgende hændelse.


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

Det er vigtigt, at alle typer data, der er føjet til samlingen med delte variabler, kan serialiseres, ellers kan serveren ikke vide, hvordan data serialiseres, og kørslen af plug-ins vil mislykkes.

For at en plug-in, der er registreret i fase 20 eller 40, kan få adgang til de delte variabler fra en fase 10 registreret plug-in, der udføres ved oprettelse, opdatering, sletning eller ved en RetrieveExchangeRateRequest, skal du have adgang til samlingen ParentContext.SharedVariables. I alle andre tilfælde indeholder IPluginExecutionContext.SharedVariables samlingen.

Se også

IPluginExecutionContext
Plug-in-udvikling
Repræsentation i plug-ins
Pipeline for hændelseskørsel

Microsoft Dynamics 365

© 2017 Microsoft. Alle rettigheder forbeholdes. Ophavsret