Concatenate two fields into a 3rd field Dynamics 365 Plugins Lesson 2

Skip to step 11 if you have already completed Lesson 1. Everything is the same except for the code.

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 concatenate two fields and fill in another.

  • 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!


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 30-49.

  • You do not need to edit the code yet. Just examine it so you can see what you will change after we locate the logical names of your fields.

  • You can see that I am creating two variables with type string, firstName and lastName.

  • In Dynamics we know that last name is a required field and first name is not. We will need to check to see if the non required field, first name, is present in the attribute collection, aka if the field contains data. If we don't, the plugin would fail if someone did not enter the first name.

  • Then we get the value of first name using the field's logical name, "firstname" and set it equal to the firstName variable.

  • After closing that "if" statement, we will get the last name value using the logical name, "lastname" and set it equal to the lastName varible.

  • Now we will fill in the description field.

  • entity.Attributes.Add() has two parameters, field and value, separated by a comma.

  • Before the comma, we identify what field we would like to fill in with its logical name, surrounded by quotation marks.

  • After the comma, we concatenate the word "Hello " (with a space) with the first name and last name variables separated by another space. Each piece is concatenated with + symbols.

  • The next few steps will show you how to find logical names for any field. Then come back and edit your code.

try

{

//Create two variables

string firstName = string.Empty;

string lastName = string.Empty;


//Check to see if the non required field/attribute exists in the attribute collection

//This makes sure your plugin will not fail if first name is not entered

if (entity.Attributes.Contains("firstname"))

{

//set the variable first name equal to the value from Dynamics

firstName = entity.Attributes["firstname"].ToString();

}

//Required fields are present in the attribute collection on create

//so we do not need to check for their presence

lastName = entity.Attributes["lastname"].ToString();


//Fill in a field/attribute with the values above.

entity.Attributes.Add("description", "Hello " + firstName + " " + lastName);

}


Step 12 - Pick a field and find its logical name

  • I am going to fill in the description field on the Contact Entity with the first and last name values. Use the following steps to find all your logical names.

  • 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 listed below the Display Name and copy it. This is the Logical Name. See the image below.

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

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

try

{

//Create two variables

string firstName = string.Empty;

string lastName = string.Empty;


//Check to see if the non required field/attribute exists in the attribute collection

//This makes sure your plugin will not fail if first name is not entered

if (entity.Attributes.Contains("firstname"))

{

//set the variable first name equal to the value from Dynamics

firstName = entity.Attributes["firstname"].ToString();

}

//Required fields are present in the attribute collection on create

//so we do not need to check for their presence

lastName = entity.Attributes["lastname"].ToString();


//Fill in a field/attribute with the values above.

entity.Attributes.Add("description", "Hello " + firstName + " " + lastName);

}

Change this code to fit your needs, but remember the following.

  • For non required fields, you must check to see if it is present in the attribute collection, aka if it contains data.

  • Use variables that match the field's logical name.

  • Use camel casing for your variables.

  • Always use the correct logical names, or your code will not work.

  • Note that I am concatenating the word "Hello " in as well. This can removed or replaced with a different word or phrase.

  • Note that I am placing a space " " between my variables. You most likely need to do the same.

  • There is no limit to the number of variables you can use.

  • The logical name of custom variables will start with a prefix like "new_", this must be included. "new_fieldname".

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.