How to register Plugin Dynamically?
Problem Definition How to register the steps of the plugin Dynamically (programmatically). Introduction Generally, after the plugin development, users register the plugin and steps using plugin registration tool provided by the CRM SDK. But, in certain cases it is possible that the user may need to register the step in the plugin dynamically. For example: Whenever a new entity is added it should have an auto number step registered automatically for that entity. Description Here I have created a sample plugin with no logic. I will register this plugin programmatically using console app. To register the plugin programmatically. We need the following steps to be executed. Connect to CRM Create a record of type “pluginassembly” Create a record of type “plugintype” Create a record of type “sdkmessageprocessingstep” Connect to CRM Since we are registering the plugin using console application we need to connect the CRM instance before processing the steps. Organization service is required to create a record in CRM. Please make sure you add the below references to the code. using Microsoft.Crm.Sdk.Messages; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Tooling.Connector; using System; using System.IO; using System.Reflection; You can refer the below code the connect with CRM public void ConnectCRM() { string connectionString = “Url=https://XXXX.crm.dynamics.com; Username=XXXX.onmicrosoft.com; Password=**********; authtype=Office365″; // Connect to the CRM web service using a connection string. CrmServiceClient conn = new CrmServiceClient(connectionString); // Cast the proxy client to the IOrganizationService interface. _orgService = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy; // Obtain information about the logged on user from the web service. Guid userid = ((WhoAmIResponse)_orgService.Execute(new WhoAmIRequest())).UserId; } Create a record of type “pluginassembly” Once the connection is successful, we need to register the Plugin Assembly. Here, we need to provide the physical path of the .dll that we need to register. While creating a record we need to provide the required field value which are listed below. You can find list of attributes required, by looking into the excel sheet provided by Microsoft. You can find this excel sheet in the SDK with the name “EntityMetadata” Attributes required: Code: public Guid RegisterAssembly() { string filePath = @”D:\Plugin_Dynamically\Sample.Plugin.Dynamically\Sample.Plugin.Dynamically\bin\Debug\Sample.Plugin.Dynamically.dll”; Guid assemblyId = Guid.Empty; try { var assembly = Assembly.LoadFile(filePath); string[] props = assembly.GetName().FullName.Split(“,=”.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); Entity assemb = new Entity(“pluginassembly”); assemb[“name”] = props[0]; assemb[“culture”] = props[4]; assemb[“version”] = props[2]; assemb[“publickeytoken”] = props[6]; assemb[“sourcetype”] = new OptionSetValue(0); // 0= database; assemb[“isolationmode”] = new OptionSetValue(2); // 1= none, 2=sandbox assemb[“content”] = Convert.ToBase64String(File.ReadAllBytes(filePath)); assemblyId = _orgService.Create(assemb); } catch (Exception) { throw; } return assemblyId; } Once the Assembly is registered you will able to see the below steps in plugin Creating the plugintype record A plugintype record must be created for each plugin inside the assembly. This can be done dynamically using reflection. We need to specify the below required field You can refer the below code for the registration of Plugin Type. The assemblyId is the Guid created on the earlier step. public Guid CreatePluginType(Guid assemblyId) { Guid pluginTypeId = Guid.Empty; Entity pluginType = new Entity(“plugintype”); pluginType[“pluginassemblyid”] = new EntityReference(“pluginassembly”, assemblyId); pluginType[“typename”] = “Sample.Plugin.Dynamically.Dynamic”; pluginType[“friendlyname”] = “Sample Plugin Dynamically”; pluginType[“name”] = “Register Plugin”; pluginType[“description”] = ” Sample Plugin Dynamically”; pluginTypeId = _orgService.Create(pluginType); return pluginTypeId; } This is how it will look like Creating the sdkmessageprocessingstep record At the end we need to create the step to process the request. For example, if you want to register the plugin for the create of the account entity. You need to have two id retrieved from CRM separately. Sdkmessageid: you will get this in from the sdkmessage entity you need to provide name as shown below. Sdkmessagefilterid: You will find this id using Query shown in the below screen shot You can refer the below code to perform the operation public void SdkMessageStep(Guid pluginTypeId) { Guid messageId = new Guid(“9EBDBB1B-EA3E-DB11-86A7-000A3A5473E8”); Guid messageFitlerId = new Guid(“C2C5BB1B-EA3E-DB11-86A7-000A3A5473E8”); Entity step = new Entity(“sdkmessageprocessingstep”); step[“name”] = “Sdk Message”; step[“configuration”] = “Create account record”; step[“invocationsource”] = new OptionSetValue(0); step[“sdkmessageid”] = new EntityReference(“sdkmessage”, messageId); step[“supporteddeployment”] = new OptionSetValue(0); step[“plugintypeid”] = new EntityReference(“plugintype”, pluginTypeId); step[“mode”] = new OptionSetValue(0); step[“rank”] = 1; step[“stage”] = new OptionSetValue(20); step[“sdkmessagefilterid”] = new EntityReference(“sdkmessagefilter”, messageFitlerId); Guid stepId = _orgService.Create(step); } Once the above step is executed successfully you will see the plugin as shown below Conclusion Even though we have plugin registration tool available to register the plugin using plugin registration tool we can also register the plugin programmatically in certain scenario.
Share Story :
Debug Script on Tablet/Mobile Application
Introduction: After the release of Microsoft Dynamics CRM update 1, Microsoft has removed the mobile version of form and made actual form available for the Mobile/Tablet. But, since few scripts do not work on mobile/Tablet, and work fine on the Browser, Microsoft has released the guide line that should be followed while writing the scripts. Kindly refer the below link for further details on the guide lines provide by Microsoft. https://msdn.microsoft.com/en-in/library/dn481572.aspx There are scenarios where scripts break and we are not able to debug them on phone/Tablet. Which leads to no proper error tracing by the developers. Description: The script which worked perfectly fine on the Web Browser, had issues on phone/Tablet. These scripts sometimes have more than thousand lines of code and we struggle to find what exactly is causing it to fail. Microsoft, for the same reason has now come up with a solution to debug these scripts. Follow the below steps in order to debug: Step: 1 While writing script makes sure to include the ‘debugger;’ tag in the beginning of the function or where you want to debug the code. Step: 2 Create a URL which works same as that of tablet. https://<CRMURL>/nga/main.htm?org=<OrgUniqueName>&server=<CRMURL> Step: 3: For e.g. the organization URL will look like below. Unique name of the org can be obtained from Settings -> customization -> developer resource -> unique name Before opening the above mentioned URL make sure you are already logged in the web browser else the link will not work. Step 4: You will receive the below message whenever there is a change in the customization. Makes sure to download the latest changes, it will take few seconds. Step: 5 Once the download is completed you can test your script by pressing F12 button, in our case I have registered the script on change of Phone field as you can see below. As soon as the value of Phone field changes it takes me to the debugger line. (PFB the screen shot for the same) Using this approach, you can debug the script on the desktop using the tablet version of CRM. Conclusion: We can debug the mobile version of script on the web browser by the steps provided. Which will allow better debugging than the manual one that the legacy system had.