D365 – Plug-in Deploying and Testing

In the previous blog “D365 – Pug-in Writing Code” we saw how we can write the Plug-in code. Now let us see how we can deploy and test the Plug-in.

Note: The .NET Framework used in this case was 4.7.1.

Sign the Assembly

The cloud platform only allows signed assemblies as that makes it more secure.

  • Right-click the project > Properties.
  • Click Signing.

  • Select Sign the assembly.
  • Select <new> in Choose a strong name key file.

  • Specify Key file name.
  • Optionally provide the password or uncheck Protect my key file with a password field.
  • Keep the default Signature Algorithm and click Ok.

Build the Project

  • Right -click the Project and click Build. This will create the assembly file.
  • To locate the assembly file again right-click the Project file and click Open Folder in File Explorer
  • Click bin > Debug
  • The DLL file will be available. We need to Register this DLL file into CRM

Register Plug-in

In the blog “D365 – Create Plugin Template”, we had discussed how we can install the tool “Microsoft.CrmSdk.XrmTooling.PluginRegistrationTool”.

We will now use this tool to Register the assembly we just built.

  • Browse to the tool: …\packages\Microsoft.CrmSdk.XrmTooling.PluginRegistrationTool.9.1.0.20\tools
  • Run the “PluginRegistration.exe” tool.

  • Create a new connection to the CRM environment. Provide your credentials and Login.

  • The Plugin Registration tools will show you a list of registered plug-in, which is taken care by the platform.
  • Click Register New Assembly.

  • Specify the location of the DLL in Step 1 and click Register Selected Plugins.

  • You will notice that the plugin is now registered.

  • Now we would need to register a step. Right-click on the plugin and click Register New Step.
  • Specify the following fields:
    • Message = Create
    • Primary Entity = contact
    • Event Pipeline Stage of Execution = PreOperation

  • Click Register New Step.

Test Result

  • Create a New contact, specify First Name and Last Name and Save.
  • Check the result in Personal Notes which is automatically populated.

Code Used

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Microsoft.Xrm.Sdk;

using System.ServiceModel;

namespace Tech_Quantum

{

public class HelloWorld : IPlugin

{

public void Execute(IServiceProvider serviceProvider)

{

// Obtain the tracing service

ITracingService tracingService =

(ITracingService)serviceProvider.GetService(typeof(ITracingService));

// Obtain the execution context from the service provider.

IPluginExecutionContext context = (IPluginExecutionContext)

serviceProvider.GetService(typeof(IPluginExecutionContext));

// The InputParameters collection contains all the data passed in the message request.

if (context.InputParameters.Contains(“Target”) &&

context.InputParameters[“Target”] is Entity)

{

// Obtain the target entity from the input parameters.

Entity entity = (Entity)context.InputParameters[“Target”];

// Obtain the organization service reference which you will need for

// web service calls.

IOrganizationServiceFactory serviceFactory =

(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

try

{

// Plug-in business logic goes here.

//To read from attribute values

string firstName = entity.Attributes[“firstname”].ToString();

string lasttName = entity.Attributes[“lastname”].ToString();

//To assign data to attribute

entity.Attributes.Add(“description”, “Hello World” + firstName + lasttName);

}

catch (FaultException<OrganizationServiceFault> ex)

{

throw new InvalidPluginExecutionException(“An error occurred in FollowUpPlugin.”, ex);

}

//catch (Exception ex)

//{

// tracingService.Trace(“FollowUpPlugin: {0}”, ex.ToString());

//throw;

//}

}

}

}

}