Search code examples
c#pluginsdynamics-crm

How to prevent firing Dynamics CRM Plugin execution while updating an entity using web service?


I have implemented a plugin for my Dynamics CRM which is firing on Update message for incident entity. Also I have a web service for external users which can update just two attributes of incident entity from outside.

The problem is while external users use the web service to update entity, the plugin will fire also. I want to bind the plugin going to be fired just inside CRM when incident entity changed and prevent it firing by outside requests.

I checked below conditions in my plugin for preventing infinite loop and it works but not works for preventing firing by outside update requests.

    if (context.Depth > 1 || 
        context.Mode != 1 || 
        context.MessageName != "Update" || 
        context.IsolationMode != 1)
    {
        return;
    }

To register plugin I've used the Plugin Registration Tool and I have set the step message to Update, and Run in User's Context as Calling User.

enter image description here

In my web service I've used Xrm.Sdk and Xrm.Sdk.Client to connect to CRM and update entity directly.

        ColumnSet cs = new ColumnSet(new string[] {
                "description", "statuscode"
            });            
        Guid recordId = new Guid(caseID);            
        Entity currentRecord = crmService.Retrieve("incident", recordId, cs);
        OptionSetValue osv = new OptionSetValue(1);
        currentRecord["statuscode"] = osv;
        currentRecord["new_answers"] = answer;
        currentRecord["new_lastanswerdate"] = currentDate;
        crmService.Update(currentRecord);

Anyone has any idea - How can I prevent plugin firing while an entity updated from outside of CRM?


Solution

  • The plugin executes in every server transaction & it gets triggered which is the expected behavior (that's the whole purpose).

    You may use some other flag (additional attribute or any service account) which only get updated/used by outside integration, in that case you can check in execution context/target entity and ignore the further execution.

    For external integration - you should create an Application user (non-interactive service account). Read more