Integrating Dynamics 365 CRM with MuleSoft Using a Synchronous C# Plugin

 

πŸ”—πŸš€ Step 1: Set Up Your C# Plugin in Dynamics 365 CRM

1️⃣ Create a New C# Plugin Project in Visual Studio

  1. Open Visual Studio → Create a Class Library (.NET Framework) Project
  2. Install the Microsoft.CrmSdk.CoreAssemblies NuGet package
  3. Add a new class called LeadCreatePlugin.cs

πŸš€ Step 2: Write the Synchronous C# Plugin Code

πŸ”Ή Plugin Code to Capture New Leads & Send Data to MuleSoft

using System; using System.ServiceModel; using Microsoft.Xrm.Sdk; using System.Net.Http; using System.Text; public class LeadCreatePlugin : IPlugin { public void Execute(IServiceProvider serviceProvider) { // Get the execution context IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); // Run the plugin only when a lead is created if (context.MessageName.ToLower() != "create") return; // Retrieve the Lead entity data if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { Entity lead = (Entity)context.InputParameters["Target"]; // Extract necessary fields string firstName = lead.Contains("firstname") ? lead["firstname"].ToString() : ""; string lastName = lead.Contains("lastname") ? lead["lastname"].ToString() : ""; string email = lead.Contains("emailaddress1") ? lead["emailaddress1"].ToString() : ""; // Prepare JSON payload for MuleSoft API string jsonPayload = $"{{ \"firstName\": \"{firstName}\", \"lastName\": \"{lastName}\", \"email\": \"{email}\" }}"; // Send Data to MuleSoft API SendDataToMuleSoft(jsonPayload); } } private void SendDataToMuleSoft(string jsonPayload) { try { using (HttpClient client = new HttpClient()) { client.BaseAddress = new Uri("https://your-mulesoft-api.com/send-leads"); client.DefaultRequestHeaders.Add("Authorization", "Bearer YOUR_ACCESS_TOKEN"); HttpContent content = new StringContent(jsonPayload, Encoding.UTF8, "application/json"); HttpResponseMessage response = client.PostAsync("", content).Result; // Synchronous call if (!response.IsSuccessStatusCode) { throw new InvalidPluginExecutionException($"Error sending data to MuleSoft: {response.ReasonPhrase}"); } } } catch (Exception ex) { throw new InvalidPluginExecutionException("Error in sending data to MuleSoft: " + ex.Message); } } }

πŸš€ Step 3: Deploy the Plugin in Dynamics 365 CRM

1️⃣ Sign & Build the Plugin DLL

  1. In Visual Studio, go to Project PropertiesSigning → Enable Sign the Assembly
  2. Build the Project (Ctrl + Shift + B) → This will create a .dll file

2️⃣ Register the Plugin in Dynamics CRM

  1. Open Plugin Registration Tool
  2. Click Register New Assembly → Upload your .dll file
  3. Click Register New Step
    • Message: Create
    • Primary Entity: lead
    • Execution Mode: Asynchronous (Recommended) or Synchronous
    • Stage: Post-operation

πŸš€ Step 4: Test the Integration

πŸ”Ή Create a Lead in Dynamics 365

  1. Go to CRM → Sales → Leads
  2. Click New, fill in details & save
  3. The Plugin will send the Lead Data to MuleSoft API

🎯 Summary – What We Achieved?

Captured New Leads in CRM using a C# Plugin
Sent Lead Data to MuleSoft via an API (without async/await)
Deployed & Tested the Integration

Now, whenever a new Lead is created, the data is automatically sent to MuleSoft, which can then forward it to any other system (like SAP, Salesforce, or a database).

πŸš€ Want real-time updates? Use Webhooks in CRM to call MuleSoft instantly instead of using a plugin!

Comments

Popular posts from this blog

πŸ€– Copilot vs Microsoft Copilot vs Copilot Studio: What’s the Difference?

Automating Unique Number Generation in Dynamics 365 Using Plugins

In-Process vs Isolated Process Azure Functions: What’s the Difference?