How to bypass plugins in Dynamics 365/ DataVerse

This is one of the best stuffs I learnt today from the community and thought of sharing with my blog readers. And trust me it’s not a click bait article. You heard me right. Imagine you running a data migration and wished plugins won’t execute during data migrations. You can now do that.

In this blog I am going to cover the below scenarios.

  • You want to bypass plugins in DataVerse/ Dynamics 365 while running bulk operations using SDK
  • You want to bypass plugins in DataVerse/ Dynamics 365 while running operations using WebAPI endpoint.

I create a sample plugin which I register on Pre-create of account entity. Below is the sample code for the plugin.

Quite simple code. All I am doing is to set the “Account Number” field of the plugin to a random number. Quite obvious, whenever I create an account, the account number is set to a random value since the plugin is execution.

Now say I have requirement where I am doing some bulk operation using SDK and I want to set the account number of the field during the bulk operation. I want the plugin should not execute during that time. Below is the code on how we can do it.

CrmServiceClient client = new CrmServiceClient(
            ConfigurationManager.ConnectionStrings["MyCDSServer"].ConnectionString);

         if (client.IsReady)
         {
            var account = new Entity("account");
            account["name"] = "Plugins will not fire for this account";
            account["accountnumber"] = "111111";

            CreateRequest createEnt = new CreateRequest();
            createEnt.Parameters["BypassCustomPluginExecution"] = true;
            createEnt.Target = account;

            client.Execute(createEnt);
         }

Very simple code again. I use XrmTooling connection string to connect to my CDS instance and then create an account with name and accountnumber specified. Since I don’t want to execute the plugin registered on-precreate, I use the below code,

createEnt.Parameters[“BypassCustomPluginExecution”] = true;

As expected the plugin didn’t fire and the accountnumber field is set to the value specified during the time of create.

I have tested with quite a few SDK messages and this parameter seem to work with all of them. Didn’t try out all the messages but certainly shall like to know if there is an exception to this.

Also if you are using CrmServiceClient and want to bypass plugin execution for all requests using the CrmServiceClient object, you can set the CrmServiceClient property to true.

client.BypassPluginExecution = true; // client is an instance of CrmServiceClient object

Now that it worked, let’s dig a little dip. The previous code I tried with someone having System administrator. Obviously you cannot draw a conclusion with System Administrator security role. After all it is like working in GOD MODE and we are human afterall.

So I tried with another user to execute the same code with less privileged role. I made sure the user have permission to create the account. However when I run the code, I get the below error.

“‘Principal user (Id=bfe59907-221c-eb11-a813-000d3a9c4acb, type=8, roleCount=1, privilegeCount=409, accessMode=0), is missing prvBypassCustomPlugins privilege”

WOW! So there is a privilege prvBypassCustomPlugins. You cannot set this privilege using UI. You need to use the following code to set this privilege to a role.

static void AddprvBypassCustomPluginsToRole(IOrganizationService service, Guid roleId)
{
    var request = new AddPrivilegesRoleRequest
    {
        RoleId = roleId,
        Privileges = new[]{
            new RolePrivilege{
                PrivilegeId = new Guid("148a9eaf-d0c4-4196-9852-c3a38e35f6a1"),
                Depth = PrivilegeDepth.Global
            }
        }
    };
    service.Execute(request);
}

My next test was to check if this can be achieved using WebApi endpoint. For that I used POSTMAN. Well it worked. All I needed to do was pass a header key “MSCRM.BypassCustomPluginException” and set the value to true.

My suggestion here would be to know about this from knowledge perspective and hold on to implementations till the official announcement. It won’t be nice for your customers to get elated only later knowing that it’s not going to work out.

You may also like the below posts

Hope this helped!

Debajit Dutta
Business Solutions MVP


Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe to get the latest posts sent to your email.

1 thought on “How to bypass plugins in Dynamics 365/ DataVerse”

  1. Thank you! BypassCustomPluginException will be very useful.
    I hope MS make this as a supported parameter.

Comments are closed.

Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading