首页 > 其他分享 >Plugins Development in Dynamics 365 CRM

Plugins Development in Dynamics 365 CRM

时间:2024-04-25 17:22:05浏览次数:13  
标签:Development Now Plugin ll plugin Dynamics 365 click Once

 Part 1 – Setting up Visual Studio Project

 

Pre-Requisites

Here’s what you need to be have installed in order to proceed to writing a plugin –

  1. Plugin Registration Tool – Required for your to connect to the Dynamics 365 environment and deploy your plugin on.
    How to get Plugin Registration Tool: Download Plugin Registration Tool for Dynamics 365 CRM using PowerShell
  2. Microsoft Visual Studio – You’ll need to write your plugin in a Visual Studio IDE as it’s the preferred way to code your plugin and also to Version Control / Source Control.

Scenario

Now, before you decide why you have to write a plugin, it is presumed that you have some high-level understanding as to why you are writing this plugin.

To keep it short, here are some points –

  1. Understand Plugin Execution Pipeline (Basically, it means when is the plugin supposed to run when you perform a certain operation) – https://carldesouza.com/dynamics-365-plugin-execution-pipeline/

Example Business Scenario

Here’s a quick example we’ll consider which we will implement using a plugin –

  1. An account has a custom field called as Group Code.
  2. So whatever is updated in this field should be updated on all the Child Contact records under the Account.


    Now, let’s write a quick plugin to implement this.

 

Writing a Plugin – Visual Studio

Given that you want to start writing a plugin, you must also have understood some concepts –

  1. You’ll need to create a new .NET Framework Project in order to get started. I use Visual Studio 2022 (This was newly released at the time of writing this post)
    So the type of Project required is a Class Library of .NET Framework
  2. Once you click Next, provide a relevant name to the Project. As a D365 Consultant working on multiple projects, you should be able to identify the library/assembly by the name as to which Org and what Module it belongs to.
    In my case, I’m calling it CFT158SalesPlugins which is sufficient to give an idea of what org and what module this assembly is for.
  3. Once I click on Create, an Empty Project with the standard Class1 will be created which will be ready for you to start writing.
  4. Next, since Dynamics 365 CRM plugins extend IPlugin interface provided by Microsoft Dynamics, you’ll need to fetch references for the same.



    Now, right-click on the Project itself and click on Manage NuGet Packages…
  5. Then, go to Browse and then search for Dataverse…
  6. You’ll find Microsoft.CrmSdk.CoreAssemblies which you can download. I usually choose a little older version than the current one.
  7. Make sure you have selected the correct one. Then, click OK.


    Next, you get a chance to also look at the licensing terms before you Accept. Once you are OK, you can click on I Accept.
  8. If you have the Output window open in your Visual Studio, you’ll be able to see the progress. It only takes a few seconds for the references to be imported.
  9. Once done, you can see that the references required have now been added to the project.
  10. I’ll rename my Class1.cs to something that gives an idea of what the Plugin would do.
    So, for example, I’m setting it to AccountUpdate.
    Detailed post on Renaming or Deleting a Plugin in Dynamics 365 CRM
    Now, that my class is renamed, I’ll also add the references to the Class File so that I can use the IPlugin interface. In most common Plugin scenarios, you’ll need Microsoft.Xrm.Sdk amongst other references too.
    My personal practice is to include as shown below –


Setting Initial Methods

Now, let’s set a template of which we can start to code the plugin. Before we do that, some common Methods need to be added first.

  1. I’ll now extend the IPlugin interface to the class.
  2. As you choose the IPlugin, in Visual Studio, you’ll be prompted for some actions which will auto-complete the interface process. Look for the icon on the left hand side and expand the menu to find Implement Interface


    Once done, the Execute method will be populated as below. Execute method is the first method from where the plugin execution starts.
    Also, make sure the class is public so that we can register is successfully in the Plugin Registration Tool.
  3. Now, below are some helper methods which are required for the plugin to be registered on the environment.
    You can copy from here:

    context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    service = ((IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory))).CreateOrganizationService(context.UserId);
    trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));




  4. In order to understand what these methods are, you can simply hover over these and read the definition. If you don’t understand these right away, it’s not a problem. But, it’s recommended that you thoroughly understand the purpose behind each of these.
  5. Then, this should be the first method to call in order to establish connection with the Dynamics 365 CRM organization you are hosting this plugin on.
  6. Next, you’ll need to Sign the Assembly before it could be registered on the environment using Plugin Registration Tool.
    Right-click on the Project and then choose Properties.
  7. Once in Properties, look for the Signing section on the left-hand side on the menu.
    Notice that the Sign the assembly is not yet enabled.
  8. Once you tick it, it’ll expose the area below and you’ll be able to create a new strong key name file.
  9. When I click on new, I created a strong name key file –
    Note: I’ve not password protected the Strong Name Key I’m creating but it’s up to you to maintain it if needed.
  10. Once I click OK, the .snk file will appear here.
  11. Finally, build the Project at this stage and we’ll move towards Registering this Plugin.

    Now, at this point, we have established the code which is ready to be hosted on the Dynamics 365 CRM organization itself. Post this point, you’ll now code the logic which is supposed to be done by the plugin.

 

 Part 2 – Registering your plugin

 

Log into Plugin Registration Tool

Based on where you have downloaded the Plugin Registration Tool, navigate to the folder and follow the below –

  1. Open the Plugin Registration Tool from the directory.
  2. Now, given that this is Dynamics 365 CRM Online (Customer Engagement / Dataverse) environment, you’ll make sure Office 365 is selected and then you’ll need to enter Admin Credentials.
  3. Once successfully authenticated, you’ll be asked to choose which organization this is in. Given that development should ideally be done on a Dev/Sandbox environment, you’ll select the appropriate environment here.
  4. Once logged in, you’ll see the the assemblies registered with the CRM environment. These are the out-of-the-box assemblies which are not supposed to be manipulated.

 

Register your Assembly

Let’s register the Plugin Assembly –

  1. Click on the Register New Assembly button and click on New Assembly
  2. You can now click on the three dots in order to navigate to the .DLL file which was generated when you Build the project.
  3. You’ll then need to navigate to the bin folder (Either Debug or Release)
  4. Once you select the .dll file, you’ll see the plugins which are exposed in the dll i.e. the Plugins of .CS of ‘public’ visibility.
    Once this looks good, click on the Register Selected Plugins
  5. Upon successful registration, you’ll see the below message.
  6. And your Assembly will now be registered successfully using the Plugin Registration Tool.


  7. In case this Project was not signed with .snk file, you’d see the below message.
    “Assemblies containing Plugins must be strongly signed. Sign the Assembly using a KeyFile“


    Now, the assembly has been registered. Next, we’ll add a step (you can call it a trigger for the plugin to start executing)

Register Step for Plugin

Now, let’s register a step for the Plugin –

  1. Now, given that the Assembly was successfully Registered, the next step is to Register a Plugin Step.
    Expand the Assembly, then expand the Plugin itself to which you want to add a Step.
  2. As per the scenarios from Blog 1, the plugin is supposed to be triggered on change of the Account’s field Group Code.
    So, when you click on Register New Step, you’ll get to configure the Step
    Here, Message means the “operation” on occurrence of which the Plugin should be triggered. In this example, we want to trigger the plugin on an Update operation.
    Primary Entity denotes which entity is this targeted at. Account in this example.
    Then, the Filtering Attributes is available if the Message is set to Update since Filtering Attribute let’s you select which all fields should be listened to in order to run/trigger the plugin.

  3. Clicking on the three dots, you’ll get to choose the fields. In this example, we’ll select the Group Code field.
  4. Once you click OK after choosing what all fields the plugin should fire on, you should select “under whose security context should the plugin be running”
    Run in User’s Context is that field to choose the user.
    In my practice, I choose either Calling User or a System Administrator
  5. Finally, I’ll choose “when in the context of the operation” should the plugin be triggered.
    Since, I want the plugin to run “after” the “Update” operation is performed, I’ll select PostOperation.
    And Execution Mode has options – Asynchronous / Synchronous defines if the plugin will run in the background or with the operation itself.
  6. Once I choose these preferences, I’ll click on Register New Step and the Step will be registered on the Plugin.
  7. At this point, you’ve successfully registered the plugin assembly and a step on the plugin to be triggered on the update of the Account field – ‘Group Code’.

Part 3 – Adding Logic

Context of the Plugin

Since this plugin is registered on one of the common messages like Update, let’s capture the context of the Update record we are working on –

  1. I’ll write a new method to just capture the context. If you are an expert in C#, you could smartly implement this than I have done.

    标签:Development,Now,Plugin,ll,plugin,Dynamics,365,click,Once
    From: https://www.cnblogs.com/lingdanglfw/p/18158189

相关文章

  • 【Docker系列】Section 2: Creating Kubernetes Development Clusters, Understandi
    引言:在Section2中,我们将转移到Kubernetes集群和对象。本节的第一章将解释如何使用一个流行的工具来创建库集群,称为KinD。我们将解释如何创建不同的网络集群,其范围从single-node(单节点)集群到使用HAProxy作为工作节点的负载平衡器的multiple-node(多节点)集群。通过一个可工作......
  • Dynamics 365(三)WEB资源使用
    在Dynamics365可以通过WEB资源来对按钮/表单进行自定义控制一、添加图片/网页/脚本通过解决方案->Web资源->新增进行 二、网页的使用选择需要的实体窗体,插入Web资源三、按钮配置通过此页面>自定义->导入解决方案 可以看到有Home,Form和SubGri不同的按钮显示,......
  • D365虚拟机安装
    原本有本地VM是2023.3.31安装的,奈何微软不断升级,导致程序一些新特性用不到,例如:1,MasterPlanning---> PlanningOptimization,2,missing'SysRowVersionNumber'systemfieldofdatatype'AxTableFieldInt64'andextendeddatatype'SysRowVersionNumber'.最开始考虑......
  • Dynamics CRM 2013 批处理
    GetServiceusingSystem;usingSystem.Configuration;usingMicrosoft.Xrm.Client;usingMicrosoft.Xrm.Client.Services;usingMicrosoft.Xrm.Sdk;usingMicrosoft.Xrm.Sdk.Query;namespaceTest{classProgram{staticvoidMain(string[]args)......
  • 题解:P10365 [PA2024] Kraniki(评分:8.4)
    前言我们一场模拟赛的题,结果原题是新鲜出炉的。小弟不才,感觉这题是做过的题中几乎最复杂的了。既然搞懂了,就来写一发题解吧。(题外话:目前最优解,我的常数真是小小又大大啊)"Upanddown,glowin'round..."Solution1、一个经典的Trick直接模拟每一种情况显然不可取,考虑计算每......
  • [Testing adn BDD] Introduction to Test and Behavior Driven Development
    TheImportanceofTestingThevalueoftesting"Ifit'sworthbuilding,it'sworthtesting.Ifit;snotworthtesting,whyareyouwastingyourtimetoworkngonit?"--ScottAmbler,agiledate.orgDesignprinciplesforApolloUsea......
  • D365 - C#计算两个时间点之间的节假日和调休
    其中调休配置在了new_holidayconfig这张表,表里还有国家和日期字段,如果不是D365可以用其他办法去存。 publicstaticintWorkingDayCount(DateTimestartTime,DateTimeendTime,stringcountryId,IOrganizationServiceadminOrganizationService){in......
  • GUI development with Rust and GTK4 阅读笔记
    简记这是我第二次从头开始阅读,有第一次的印象要容易不少。如果只关心具体的做法,而不思考为什么这样做,以及整体的框架,阅读的过程将会举步维艰。简略记录gtk-rs的书中提到的点。对同一个问题书中所演示了多种处理方法,而且跨度比较大,第一次阅读的时候经常出现忘记之前的内容。f......
  • 微软安全开发流程(Security Development Lifecycle, SDL)
    1.SDL简介SecurityDevelopmentLifecycle(SDL)是微软2004年提出的从安全角度指导软件开发过程的管理模式。SDL是一个安全保证的过程,其重点是软件开发,在开发的所有阶段都引入了安全和隐私的原则。SDL一直都是微软在全公司实施的强制性策略。2.SDL步骤SDL中的方法,试图从安全漏......
  • P3654 First Step (ファーストステップ)
    题目链接:本题数据范围仅为\(100\),因此可以暴力枚举\(O(n^3)\),唯一需要注意的一点就是当\(k=1\)时,横着站和竖着站是一样的,答案被计算了两次,因此最终的\(\rmans\)需要再除以\(2\)。#include<bits/stdc++.h>constintN=110;charw[N][N];intR,C,K,ans;boolfl......