top of page
Writer's pictureDrew Clark

How to create a plugin in Microsoft Dynamics 365: Lesson 1 Fill in a field on create of contact.

Updated: Oct 3, 2019

You do not need to have any experience with coding! I will do my best to walk you through every step and if you could, please provide feedback in the comments section at the bottom.

Unlike most tutorials we will jump straight in, get your feet wet and get to the import explanations in later lessons.


What you will learn

  • Setup Visual Studio

  • Create a publisher in Dynamics

  • Create a new solution to hold your customizations

  • Add existing system entity to your solution

  • Create a project in Visual Studio

  • Use NuGet Package Manager (NPM) to install SDK Core Assembly and Plugin Registration Tool

  • Add a reference to Microsoft.Xrm.Sdk.dll

  • Add a reference to System.ServiceModel

  • Use C# example code

  • Pick a field and find its logical name

  • Write code to fill in the job title field/attribute or one of your choosing

  • Sign the assembly

  • Build the solution

  • Get the finished plugin assembly file path

  • Open the Plugin Registration Tool

  • Register the Plugin

  • Register New Step for the plugin

  • Test the plugin

Setting up Visual Studio & Dynamics for development

Step 1 - Download Visual Studio Community

  • You need to download Visual Studio Community (its free). Just click download, this will download the stable version. Stable version: A stable release is a version that has been tested as thoroughly as possible and is the most reliable version.

Step 2 - Install Visual Studio with required workloads

  • Double click the downloaded folder and run the Visual Studio Installer.

  • On the installer window, please select the following

  • On the Workloads tab, within the Desktop & Mobile section, select .NET desktop development

  • On the same tab, but under the Web & Cloud section, select ASP.NET and web development

.Net desktop development Visual Studio Workloads
  • Click the Individual Components tab, under the Development Activities section, select Windows Workflow Foundation. Do not uncheck the other items that autocheck.

  • Click Install in the bottom right corner. This will take a while.

Step 3 - Open Visual Studio

  • Open Visual Studio

  • If a prompt pops up asking to sign in, click "Not now, maybe later." unless you have a Microsoft account.

  • If a prompt pops up labeled "Start with a familiar environment", Change Development Settings to Visual C#, then click "Start Visual Studio".

Step 4 - Create a new publisher in CRM.

  • Click Settings, select "Customizations" under the Customization section.

  • Click Publishers, Click New in the top left corner.

  • Type in a display name, like your first name, I am going to use "Drew"

  • Under prefix, change "new" to 2 or more letters that will represent your customizations. I am going to use "dr". If I were to create a field for city, the schema name would show "dr_city", ignore this for now if you do not know what that is.

  • Click Save and Close in the top left corner.

Step 4 - Create a new solution to hold your customizations in CRM.

  • Click Settings, select "Solutions" under the Customization section.

  • Click Create New in the top left corner.

  • Type in a Display Name, I am going to use "Examples", you may want to use "Plugins" or your name.

  • Click into the publisher field and type the name of the publisher you created and press enter. From the drop down select your publisher.

  • Type "1.0.0.0" for the version

  • Leave Configuration Page Blank

Microsoft Dynamics 365 New Solution Setup
  • Click Save in the top left corner

Step 5 Add Existing Contact Entity

  • On the Solution you just created, click the Add Existing button, you should see a drop-down appear.

  • Select Entity.

Dynamics 365 Add Existing Entity
  • Scroll down and select "Contact". Click OK

Dynamics 365 Add Existing Contact Entity
  • On the next page check off "Add All Assets" in the top right corner and Click Finish

Dynamics 365 Add All Assets to Existing Entity
  • On the next page titled "Missing Required Components"

  • Select "No, do not include required components."

  • Click OK

Dynamics 365 No, do not include required components

You finished the setup!


Write a Plugin! Fill in a field on create

Step 6 - Create a project in Visual Studio

  • Click File, Select New, Select Project

  • Select Class Library (.Net Framework) from the list.

  • DO NOT PICK "Class Library (.Net Standard)".

Dynamics Create a new project Class Library (.NET Framework)
  • Give the project a name like "Plugins".

  • Give the Solution another name like "PluginsSolution" or leave them the same.

Dynamics Plugin Configure New C Sharp Class Library (.NET Framework)
  • Click OK, Your new project will appear!

Step 7- Change the default class name and csharp file name, use pascal casing for classes and file names!

  • 7A - Change the default class name "Class1" to match the purpose of the plugin. I replaced it with "FillAField", because this plugin will be used to fill a field.


Each class represents a plugin. Each Class file could have many plugins, but for organization and professionalism purposes create a new class file for every plugin. This will greatly increase the organization of your plugins.

To add another class file, right click "Plugins", Select Add, then click "Class..." at the bottom.


Step 8 - Add SDK assembly from NuGet Package Manager (NPM)

  • Select Tools from the ribbon at the top of the window.

  • Select NuGet Package Manager and then click "Manage NuGet Packages for Solution"

  • Click the "Browse" tab

  • Type Microsoft.Crmsdk.CoreAssemblies in the search box

  • Select it from the list below, and then check the box next to Plugins on the right.

  • Change the Version to 9.0.2.5 and Click Install

  • If a pop up window appears called preview changes, click OK

  • If another window pops up called license agreement, Click "I Accept"

  • We need to install a second item, while still in NuGet Package Manager,

  • Type Microsoft.CrmSdk.XrmTooling.PluginRegistrationTool into the search box

  • Select it from the list below, and then check the box next to Plugins on the right.

  • Change the Version to 9.0.2.5 and Click Install

  • If a pop up window appears called preview changes, click OK

  • If another window pops up called license agreement, Click "I Accept"

Close NuGet Package Manage in the top left corner of its window.

Step 9 Add a reference to Microsoft.Xrm.Sdk.dll

  • In the Solution Explorer, right click on the Project called "Plugins".

  • Select "Open Folder in File Explorer" at the bottom of the list.

When the File Explorer opens, click the folder to the left of plugins called "PluginsSolution" in the folder path ribbon shown below.

Now that you are in the folder above, click packages

  • Click on the folder labeled Microsoft.CrmSdk.CoreAssemblies.9.0.2.5

  • Then click on the lib folder, and then net452

  • Finally you will see a file called Microsoft.Xrm.Sdk.dll

  • Left click into the file path at the top of the File Explorer, and then right click to copy this path.

  • Close the file explorer

  • In the Solution Explorer, right click on References and select Add Reference

A window will pop up, Click the Browse tab on the left.

  • Click "Browse..." in the bottom right corner.

  • Paste the path you copied earlier into the File name: Search box at the bottom of the file explorer, press enter.

  • Click the Microsoft.Xrm.Sdk.dll file and then click Add in the bottom right corner.

Step 10 Add a reference to System.ServiceModel

  • In the Solution Explorer, right click on References and select Add Reference just like you did in the last step.

  • Click the Assemblies tab on the top left.

  • In the search box in the top right, search for System.ServiceModel

  • Click on the corresponding item, and click OK in the bottom right corner.

Step 11 - Replace all your code with my example code to speed things up.

  • Replace all your code with this code.

  • If you are unsure, in Visual Studio press Control + A, press delete, copy the code and paste it into visual studio.

  • Please identify the section that looks like the code below. It will be near lines 32-36. This is where you tell the plugin what field to fill in, and with what value. You can see in parentheses that I am choosing to edit the "jobtitle" field and fill it in with "Senior Vice President". Follow the next few steps to see how I found the logical name for the field I wanted to fill in and how you can fill in any field with what you want.

try

{

//Fill in a field/attribute

entity.Attributes.Add("jobtitle", "Senior Vice President");

}


Step 12 - Pick a field and find its logical name

  • I am going to fill in the Job Title field on the Contact Entity, this would also work for any other Text fields on the contact entity.

  • Go to the form where your field is located.

  • Click the three dots at the end of the ribbon and select Form.

  • Find the field you will be filling in and double click it

  • Click on the details tab, locate the Name of the field below the Display Name and copy it. This is the Logical Name

Step 13 - Write Code to fill in a different field/attribute

  • Earlier I showed the try { } block/section of code, near lines 32-36 where it says

try

{

//Fill in a field/attribute

entity.Attributes.Add("jobtitle", "Senior Vice President");

}

  • If you would like to fill in a different field, replace "jobtitle" with the logical name of the field.

  • After the comma, we enter what we would like to fill the field with. Make sure that both are enclosed with quotation marks.

Step 14 - Sign the Assembly

  • In the Solution Explorer, Right click on Plugins and select Properties at the bottom.

Select the Signing tab on the left side

  • Check the "Sign the assembly" box

  • Click into the drop-down below "Choose a strong name key file:", Select new

  • On the Create Strong Name Key window, type Key into Key file name:

  • Uncheck the box labeled Protect my key file with a password..

  • Under Signature Algorithm, leave it on the default sha256RSA

  • Click OK

  • Click the save icon in top left

  • Select the Build menu from the top ribbon, and click on Build Solution

  • You should see Build Succeeded at the bottom of the window on a blue ribbon.

  • You have now created your first plugin!

Step 15 - Get the finished plugin assembly file path

  • Right click Plugins in the Solution Explorer

  • Choose "Open Folder in File Explorer" at the bottom of the list of options

  • Double click on the bin folder

  • Double click the Debug folder

  • You should now see a file called Plugins.dll

  • This is the file we will upload into CRM

  • Copy this file path and keep it for Step 17 or just remember where this file is.

  • For now exit this window

Step 16 - Open the Plugin Registration Tool

  • Right click Plugins in the Solution Explorer

  • Choose "Open Folder in File Explorer" at the bottom of the list of options

  • Just like in step 9, when the File Explorer opens, click the folder to the left of plugins called "PluginsSolution" in the folder path ribbon shown below.


Now that you are in the folder above, double click the packages folder.

  • Double click on the Microsoft.CrmSdk.XrmTooling.PluginRegistrationTool.9.0.2.5 folder.

  • Double click on the tools folder.

  • Scroll down and double click the application labeled "PluginRegistration.exe", shown in the picture below. Notice the file path highlighted in yellow.

Step 17 - Login to the Plugin Registration Tool

  • Once the Plugin Registration Tool application opens, click on "+Create New Connection" in the top left corner.

  • Check the box labeled "Display list of available organizations"

  • Click Login

  • Sign in to your Microsoft account, its the same credentials you use for Dynamics 365.

  • A window will appear with one or more organizations/ environments available to you.

  • I am using my development environment so I am going to choose that one.

  • Remember this step, because if you want to put your plugin in to your live environment, you would choose your live organization.

  • For security purposes I have blacked out the organizations.

Step 18 - Register the Plugin

  • Click Register in the top left corner

  • Choose Register New Assembly or press Ctrl + A

  • In the first box labeled Step 1: Click the 3 dots and located the file using the file path copied at the end of Step 15.

  • Click on the Plugins.dll file and click open

  • Click the "Register Selected Plugins" button a the bottom of the window.

Click OK on the pop up window.

  • You should now see your registered plugin assembly within the plugin list.

Step 19 - Register New Step for the plugin

  • Click the arrow to the left of the plugin assembly, right click the plugin inside the assembly and choose "Register New Step" or press Ctrl + T

  • The Register New Step window will appear.

  • In the Message box type "Create"

  • In the Primary Entity box type "Contact"

  • Further down on the bottom left, change the Event Pipeline Stage of Execution to "PreOperation"

  • Click Register New Step, we will discuss the other options of that page in later lessons.

You did it! Lets test the plugin! Bring up you CRM window.

Step 20 - Test the plugin

  • Go to the Contact Entity in CRM and create a new contact.

  • Fill out the required fields and press save.

  • In the image below you can see the final result.

You have built your first plugin and tested it!

Please leave a comment below, I would really appreciate some feedback.


0 comments

Comentários


bottom of page