Comment unsuccessful. Please correct the errors below.

Packaging plugins

You spent years polishing your algorithm, moving bytes around, replacing strings with StringBuilders, extracting every last bit of performance from your code. The assembly Acme.CoolTools.dll is into its' 3rd major version, competitors are begging for mercy. Life is good. Then Microsoft CRM 4 comes along with its' beastly architecture wanting plug-ins to be deployed in a database. And offline CRM clients, clutching a copy of Reflector all want your dazzling assembly as well.

Perhaps you simply wanted to avoid GAC, which Chris Sells knew to be evil back in 2004. Or may be you just felt that instead of being sucked into assembly hell you would rather distribute one and only one assembly.

Whatever the real reason might be, today's challenge is to write a Microsoft CRM 4 plug-in that uses classes and methods from other assemblies; then package it all into one blob that can be deployed and distributed as required. In fact, it was the challenge from one of our customers as well as from the newsgroups (speaking of good timing!). [more]

Library code

Let's say we have a secret code that predicts annual turnover for a company based on the name. Here is our complex algorithm in its' entire glory.


using System;
namespace Acme.CoolTools
{
    public static class AccountMagic
    {
        public static decimal CalculateAnnualTurnover(string companyName)
        {
            return Math.Abs(companyName.GetHashCode());
        }
    }
}

The code is compiled into a separate assembly Acme.CoolTools.dll that is then referenced by our plug-in.

Plugin

The plug-in is as sophisticated: it's designed to "monitor" account creations and updates, and then to recalculate and update annual turnover using company name.


using System.Reflection;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

namespace Acme.Plugins
{
  public class AccountPlugin: IPlugin
   {
      public void Execute(IPluginExecutionContext context)
      {
          // safe-guard recursion
          if (context.Depth > 1)
              return;
          // Extract dynamic entity from postentityimage. We should have id, name and revenue
          //    properties if they are not null in the database.
          DynamicEntity entity = (DynamicEntity)context.PostEntityImages["accountEntity"];

          // Re-calculate annual turnover based on a company name. Use proven and tested algorithm.
          decimal turnover = Acme.CoolTools.AccountMagic.CalculateAnnualTurnover((string)entity["name"]);
          // Get Crm service, set revenue property and update the entity.
          ICrmService service = context.CreateCrmService(true);
          entity["revenue"] = new CrmMoney(turnover);
          service.Update(entity);
      }
   }
}

Note that we use PostEntityImages to access account information. For performance reasons this is recommended way to pass entity properties to a plug-in code instead of executing Retrieve requests left and right. As a handy side-effect, entity extracted from postentityimage is exactly what we need to update the record.

Merging the libraries

Remember how you can compile ASP.NET code into a single assembly? ILMerge is a sibling utility that can be used to merge existing assemblies into a single one. It even merges PDB files and resigns the final output. There are some GUI wrappers but, honestly, it's just as easy from the command-line; MSBuild fans can use ILMerge task.

The command to merge our two assemblies into one while maintaining strong name is quite simple:

"c:\Program Files\Microsoft\ILMerge\ilmerge" /keyfile:acme.snk Acme.Plugins.dll Acme.CoolTools.dll /out:Acme.dll

Note that we have to give it a key to resign the assembly.

Registration

Now we have a single Acme.dll assembly that needs to be registered using the latest CRM plug in registration tool:

  • Register assembly and the plugin
  • Register steps for Create and Update messages on account entity. Synchronous, Post stage, Parent Pipeline, Server. Plus Offline if required.
  • For Update message specify name as the only filtering attribute, i.e. the one that triggers plug-in execution.
  • Register post images for both steps. We only need accountid, name and revenue properties.

Registration should like the following:

image 

Summary

The code and a simple command file to build the plug-in are attached. You'll need to copy Microsoft.Crm.Sdk.dll and Microsoft.Crm.SdkTypeProxy.dll libraries into the same folder as the source files for C# compiler to find them. Alternatively, adjust references in the batch file to point to the existing location.

I has not tested Offline client but I cannot see why it would not work.

And last but not least, company named Test generates about 5.5 times annual revenue of Acme company.

AcmePlugin.zip (1.71 kb)

Posted by: George Doubinski
Last revised: 05 Dec, 2012 05:25 PM

Comments

mike
mike
06 May, 2014 08:11 AM

After a long while I got to know about your posts and here you have put up a very significant article on how you shifted to funnel web and what are the advantages of such a service. I liked the post. Thank you. It was a post worth reading

http://robertedenslawoffice.com/

asasasas
asasasas
25 Dec, 2014 08:47 AM

asasasas asasas

Keven Peters
Keven Peters
29 Jan, 2015 08:25 PM

While you are looking for an authority throughout web design plans, on the list of things you will need to take into account is the podium that you might want your internet site being built on. Most of the time, the CMS can be the most appropriate alternative, and also the most popular CMS can be WordPress. WordPress along with other CMSs have got numerous rewards with regard to small business web sites. Nonetheless, just about the most important benefits of WordPress can be we now have several extensions accessible which you'll want to include aimed at your website to enhance their features. dissertation coach

Brayden
Brayden
27 May, 2015 05:09 AM

Definitely the Microsoft CRM 4 comes with its' terrible architecture however Microsoft has been promoting Dynamics CRM as a XRM stage and has been urging accomplices to utilize its exclusive system to alter it. You can get rush my essay for your blog and through this engage your customers with article.

Drake Smith
Drake Smith
08 Apr, 2016 04:57 AM

I have got reports about the new plugins being very useful for a lot of people. It is successful in helping users on a daily basis to help them finish off their work and quick as possible with much better results too.

Interesting :: medical billing outsourcing companies

Bloggy
Bloggy
26 Apr, 2016 04:13 AM

The post on the packaging plug ins that are shared here on the site are so good. I am interested in the latest technologies and this site has helped me a lot. Keep on sharing more and more posts like this. Thank you jewish community los angeles

Yosef Adelman
Yosef Adelman
28 Apr, 2016 06:41 AM

The post on the various browser plug-ins that are shared here on the page are so good. I like to know more about technologies and this site has helped me a lot. Keep up the good work guys. Thank you hotels on catalina island

tammy philip
tammy philip
17 Jun, 2016 05:01 AM

After a long while I got to know about your posts and here you have put up a very significant article on how you shifted to funnel web and what are the advantages of such a service. I liked the post. Thank you. It was a post worth reading

capture snapchat screen shot

tammy philip
tammy philip
17 Jun, 2016 05:03 AM

After a long while I got to know about your posts and here you have put up a very significant article on how you shifted to funnel web and what are the advantages of such a service. I liked the post. Thank you. It was a post worth reading

capture snapchat screen shot

[url= http://www.tinderpcdownload.com/best-hookup-apps-like-tinder/]hi[/url]

16 Aug, 2016 08:19 AM

That is the excellent mindset, nonetheless is just not help to make every sence whatsoever preaching about that mather. Virtually any method many thanks in addition to i had endeavor to promote your own article in to delicius nevertheless it is apparently a dilemma using your information sites can you please recheck the idea. thanks once more. nba 2k16 apk

Ross
Ross
19 Oct, 2016 08:24 PM

If you want to use tinder without Facebook then visit tinder app without facebook

Liya
Liya
27 Oct, 2016 12:08 PM

You have given a very informative blog about the packaging plugins and it helped me to understand more about it. Thanks for sharing the complete details about the program and its registration process. Expecting more informative blogs like this rv parks in harlingen tx

01 Mar, 2017 07:20 PM

Thanks for the article. Here Play the game with your kids and children in free tim by asking questions related to everything would you rather for kids

sai sree
sai sree
13 Jun, 2017 12:19 PM

http://www.fathersday2017quotes.com/

ranjith kumar
ranjith kumar
sai sree
sai sree
14 Jun, 2017 12:39 PM

blue waffle pictures

sai sree
sai sree
15 Jun, 2017 06:45 AM

http://www.fathersday2017quotes.com/

22 Jun, 2017 06:21 AM

Tinder sucks when using with facebook .,Use Tinder Without Facebook

Your Comments

Comment unsuccessful. Please correct the errors below.
Used for your gravatar. Not required. Will not be public.
Posting code? Indent it by four spaces to make it look nice. Learn more about Markdown.

Preview