{Tips & Tricks} Explore a managed solution’s plugin assembly programmatically in Dynamics CRM Online.

I never find a good title to explain my blog post. So I would rather suggest you read through my entire post even if blog title seems absolutely non-sense Smile.

So coming back to the topic here, first let me tell you why I needed to do all this stuff. My customer environment contained a managed solution which had a utility plugin. The solution was developed by some consultant company who was working for my customer initially. The source code for the managed solution was with the consultant firm. Unfortunately during handover, the source code handover got missed.

My client is running CRM 2015 online and all of a sudden, this managed assembly started throwing error. It contained some cool stuffs. So we cannot get rid of it. After multiple discussions with our client, we decided to rewrite the stuffs in the assembly. So we need the assembly. But how do we do it?

Since it is a managed solution, you cannot export it. After putting my head around it, I created a console application to get out the DLL. Just a few lines of code and I am pretty sure that if you are from a programming background like me, you would love this. So let’s see the code here.

private static void ReadPluginAssembly(OrganizationServiceProxy proxy)
        {
            var queryExp = new QueryExpression("pluginassembly");
            queryExp.Criteria.AddCondition("name", ConditionOperator.Like, "TestPlugins%");

            queryExp.ColumnSet.AddColumn("content");
            var result = proxy.RetrieveMultiple(queryExp);

            var base64Content = result.Entities[0].GetAttributeValue<string>("content");
            var byteContent = Convert.FromBase64String(base64Content);

            System.IO.File.WriteAllBytes(@"c:\debajit\TestPlugins.dll", byteContent);

        }

Let me explain the code here a bit. First I have queried the pluginassembly entity for the particular assembly in question here and retrieve the content field. The content field is where CRM stores the entire content of the plugin assembly as base 64 encoded string.

So I get the content and covert the base64 string to byte content. And then I saved the byte content to an assembly with the same name.

And how do I get the code from here. Use the wonderful Red-Gate .NET reflector or ILSpy and you would find much of the code. With little bit of modifications, you can just copy paste the entire code.

Hope you find this interesting.