Sample: Azure aware custom workflow activity

Applies to Dynamics 365 for Customer Engagement apps version 9.x

This sample obtains the data context from the current Dynamics 365 for Customer Engagement for Customer Engagement apps operation and posts it to the Azure Service Bus.

This sample code is for Dynamics 365 for Customer Engagement. Download the sample: Work with Microsoft Dynamics 365 for Customer Engagement and Azure Integration

Prerequisites

Internet connection is required to download the sample project and to restore the NuGet packages used in the sample project.

Requirements

You must configure Dynamics 365 for Customer Engagement to connect with Azure before registering and executing this sample custom workflow activity. More information: Configure Microsoft Azure Integration with Dynamics 365 for Customer Engagement.

Notice the “Input id” required argument in the code. When you add this activity to a workflow, you must provide the GUID of a Azure service endpoint.

When registering this custom workflow activity with Dynamics 365 for Customer Engagement, you must register it in the sandbox (partial trust).

For more information about the requirements for running the sample code provided here, see Use the sample and helper code.

Demonstrates

This sample shows how to write a custom workflow activity that can post the data context from the current Dynamics 365 for Customer Engagement operation to the Azure Service Bus. The posting of the data context is done through the Execute(EntityReference, IExecutionContext) method.

Example



// This namespace is found in the System.Activities.dll assembly.
using System.Activities;

// This namespace is found in the Microsoft.Xrm.Sdk.dll assembly
// located in the SDK\bin folder of the SDK download.
using Microsoft.Xrm.Sdk;

// This namespace is found in the Microsoft.Xrm.Sdk.Workflow.dll assembly
// located in the SDK\bin folder of the SDK download.
using Microsoft.Xrm.Sdk.Workflow;

namespace Microsoft.Crm.Sdk.Samples
{
    /// <summary>
    /// This class is able to post the execution context to the Windows Azure 
    /// Service Bus.
    /// </summary>
    public class AzureAwareWorkflowActivity : CodeActivity
    {
        /// <summary>
        /// This method is called when the workflow executes.
        /// </summary>
        /// <param name="executionContext">The data for the event triggering
        /// the workflow.</param>
        protected override void Execute(CodeActivityContext executionContext)
        {
            IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();

            IServiceEndpointNotificationService endpointService =
                     executionContext.GetExtension<IServiceEndpointNotificationService>();
            endpointService.Execute(ServiceEndpoint.Get(executionContext), context);
        }

        /// <summary>
        /// Enables the service endpoint to be provided when this activity is added as a 
        /// step in a workflow.
        /// </summary>
        [RequiredArgument]
        [ReferenceTarget("serviceendpoint")]
        [Input("Input id")]
        public InArgument<EntityReference> ServiceEndpoint { get; set; }
    }
}

See also

Sample Code for Dynamics 365 for Customer Engagement and Microsoft Azure Integration
Sample: Persistent Queue Listener
Custom Workflow Activities
IServiceEndpointNotificationService