0
votes

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?

1
Since you are using Calling User, you can get information about the user by InitiatingUserId in the plugin execution context. With the Guid, you can check your user table and determine whether they are external or notChronicle
@Chronicle Is it needed to define an specific user which is specialized for connecting to CRM by web service? If yes, which role I can assign to new user credential for restricting any other operation?Mehdi
If you make a custom role and assign only the web service user this role, then that is a way of restricting plugin functionality - check in your code if user has that role, otherwise return. But it doesn't have to be a role, you could also add a flag field to the user table, and set that flag for that user, that way you don't have to add new roles for each serviceChronicle
Check this out: Application user (non-interactive user) docs.microsoft.com/en-us/power-platform/admin/…Arun Vinoth - MVP
thanks @Chronicle, binding plugin execution to all users except the new one worked as good.Mehdi

1 Answers

1
votes

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