Programmatically registering plugins in CRM 4

The plugin registration tool is great when you want to quickly register, change and test plugins, but if you have an automatic build/testing environment you need some way to creating a new CRM organisation, importing customizations, register custom plugins/workflows and run your tests.

Below you’ll find some handy snippets of code that you can use to register plugins programmatically.

 

Request & Response

string assemblyPath = @".\blah.dll";
RegisterSolutionRequest request = new RegisterSolutionRequest

{
    PluginAssembly = GetPluginAssembly(assemblyPath),
    Steps = GetSteps()
};

RegisterSolutionResponse response = sdk.Execute(request) as RegisterSolutionResponse;
Guid solutionId = response.PluginAssemblyId;

solutionId is useful when you want to unregister the solution.

 

GetPluginAssembly Method

It’s recommended that you store the plugin it the database, one of the main reasons is if you have multiple front-ends you don’t have to copy the plugin to disk or GAC, CRM will automatically load and execute. The plugin content is stored as base64 therefor we have to read all the bytes from the dll and convert to base64.

Assembly assembly = Assembly.LoadFile(assemblyPath);
string[] prop = assembly.GetName().FullName.Split(",= ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
pluginassembly asm = new pluginassembly
{
    name = prop[0],
    culture = prop[4],
    version = prop[2],
    publickeytoken = prop[6],
    sourcetype = new Picklist(AssemblySourceType.Database),
    content = Convert.ToBase64String(File.ReadAllBytes(assemblyPath))
};

GetSteps Method

Sample code below shows how you can create a single step, to register multiple steps, create an array, store each step inside the array and assign to the Steps property of the RegisterSolutionRequest.

var step1 = new SdkMessageProcessingStepRegistration
{
    CustomConfiguration = "",
    Description = "Example Post Create",
    FilteringAttributes = ""// all attributes
    Mode = 1, // async
    MessageName = "Create",
    PrimaryEntityName = "mag_example",
    Stage = 50, // post
    SupportedDeployment = 0,
    PluginTypeName = "Xyz.Abc.ExamplePlugin",
    PluginTypeFriendlyName = "Abc - Example",
    InvocationSource = 0 // parent pipe
};

PluginTypeName is the namespace.classname of the plugin, PluginTypeFriendlyName is what’s displayed in the plugin registration tool, if you have multiple steps on the sample class make sure to keep the PluginTypeFriendlyName the same so it’s grouped nicely in the registration tool.

No Comments