Demo 11 - Build a plug-in
Objective: In this demo, you will show how to create a Dataverse plug-in to validate a Pet Trick record is associated with a Trick and a Pet.
Task 11.1 - Create plug-in
-
Create a folder on the Virtual machine named DemoPlugin and then run the following commands in the Developer command prompt.
pac plugin init
start DemoPlugin.csproj
-
Rename Plugin1.cs to ValidatePetTrick.cs.
-
Replace the code.
using Microsoft.Xrm.Sdk; using System; namespace DemoPlugin { public class ValidatePetTrick : PluginBase { public ValidatePetTrick(string unsecureConfiguration, string secureConfiguration) : base(typeof(ValidatePetTrick)) { } protected override void ExecuteDataversePlugin(ILocalPluginContext localPluginContext) { if (localPluginContext == null) { throw new ArgumentNullException(nameof(localPluginContext)); } var context = localPluginContext.PluginExecutionContext; localPluginContext.Trace("Message:" + context.MessageName); localPluginContext.Trace("Entity:" + context.PrimaryEntityName); localPluginContext.Trace("Primary Entity Id:" + context.PrimaryEntityId.ToString()); localPluginContext.Trace("Stage:" + context.Stage.ToString()); localPluginContext.Trace("Depth:" + context.Depth.ToString()); // Validate Entity and Message if (context.PrimaryEntityName != "dem_pettrick") { localPluginContext.Trace("Primary Entity is not Pet Trick"); //return; } if (context.MessageName.ToLower() != "create") { localPluginContext.Trace("Message is not Create"); return; } // Validate contents of entity if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { localPluginContext.Trace("Target"); var entity = (Entity)context.InputParameters["Target"]; if (!entity.Contains("dem_petid")) { localPluginContext.Trace("No Pet in Entity"); throw new InvalidPluginExecutionException("A pet must be selected"); } if (!entity.Contains("dem_trickid")) { localPluginContext.Trace("No Trick in Entity"); throw new InvalidPluginExecutionException("A trick must be selected"); } } } } }
-
Build the project.
Task 11.2 - Register plug-in
-
Start the Plug-in Registration Tool.
-
In the command prompt, run the command below to launch the Plugin Registration Tool (PRT).
pac tool prt
-
-
Create a connection to the Demo environment.
-
Register the DemoPlugin.dll assembly.
-
Register a step on the DemoPlugin.dll assembly.
-
Enter
Create
for Message. -
Enter
dem_pettrick
for Primary Table. -
Select PreValidation from dropdown for Event Pipeline Stage of Execution.
-
Task 11.3 - Test plug-in
-
Open a new Pet Trick form.
-
Enter a name and select Save.
-
Select a Pet and select Save.
-
Select a Trick and select Save.
Task 22.4 - Add plug-in to solution
-
In the demo solution, add existing Plug-in Assembly and select DemoPlugin.
-
In the demo solution, add existing Plug-in step and select Create of dem_pettrick.