Search code examples
web-servicespluginsmicrosoft-dynamics

Microsoft Dynamics CRM - Pass Parameters from Web Service to IPlugins


We are building some plugins in Microsoft Dynamics CRM by inheriting from IPlugin. We have these configured so they fire whenever an Account is updated.

The problem is the plugins are calling our services, which causes our service to respond with an update. We are doing some pretty hacky things right now to prevent these cyclical updates from happening.

We were wondering if there was a way to pass a value to the IOrganizationService service (the web service) that a plugin can look at. Our other system could send a flag ("hey, don't bothing sending an update!") and the plugin could skip calling back.

Can we pass parameters from web service to the plugins?


Solution

  • We decided the correct solution was to use the value found in IPluginExecutionContext.InputParameters["Target"]. In the case of an Update, this returns an Entity containing attributes for all the attributes that were updated.

    We basically have a list of attribute names we cared about. We loop through names and see if any of them appear in the entity attribute list. If so, we send an update to our other system. The good news is, Dynamics CRM ignores updates where the values don't actually change, so trying to update a value to itself is no-op.

    public void Execute(IServiceProvider serviceProvider)
    {
        IPluginExecutionContext context = serviceProvider.GetService(typeof(IPluginExecutionContext));
        Entity entity = (Entity)context.InputParameters["Target"];
        string[] fields = new string[] { "name", "statecode", "address1_line1" };
        bool hasUpdates = fields.Where(f => entity.Attributes.Contains(f)).Any();
        if (!hasUpdates)
        {
            return;
        }
    }