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”
class PluginRegister
{
public Guid RegisterAssembly()
{
CrmService.Initialize();
IOrganizationService service = CrmService.GetOrganisationService();
string filePath = @"C:\Users\fiveh\Documents\Visual Studio 2019\Project\MultiModalLogistics\MultiModalLogistics.WorkFlows\bin\Debug\MultiModalLogistics.WorkFlows.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 = service.Create(assemb);
}
catch (Exception ex)
{
throw;
}
return assemblyId;
}
public Guid CreatePluginType(Guid assemblyId)
{
CrmService.Initialize();
IOrganizationService service = CrmService.GetOrganisationService();
Guid pluginTypeId = Guid.Empty;
Entity pluginType = new Entity("plugintype");
pluginType["pluginassemblyid"] = new EntityReference("pluginassembly", assemblyId);
pluginType["typename"] = "Territory.GetTerritorybyName";
pluginType["friendlyname"] = "Territory by Name";
pluginType["name"] = "Territory by Name";
pluginType["description"] = "Territory by Name";
pluginType["workflowactivitygroupname"] = "Logistics Workflow";
pluginTypeId = service.Create(pluginType);
return pluginTypeId;
}
public void SdkMessageStep(Guid pluginTypeId)
{
CrmService.Initialize();
IOrganizationService service = CrmService.GetOrganisationService();
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 = service.Create(step);
}
}
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.
No comments:
Post a Comment