Use tag parameter to bypass plugin execution in dynamics 365

Tag parameter is a savior when it comes to situation where a particular plugin execution needs to be skipped.

Scenario - A custom api creates contact and also performs execution of certain logic. Also, there is a plugin that executes similar logic which is triggered when contact is created. This leads to conflict, as custom api & plugin both performs the execution of similar logic. At a stage when design can not be changed, arises a need to skip/bypass the execution of logic in plugin, there comes the 'Tag' parameter.

Tag parameter can be set in the request, inside the api and can be accessed using the sharedvariables inside a plugin.

API code

namespace CustomAPI{
  public partial class CustomAPI : IPlugin
        private IPluginExecutionContext context;
        private IOrganizationServiceFactory serviceFactory;
        private IOrganizationService organizationService;
        private ITracingService tracingService;
        private OrganizationServiceContext serviceContext;
        public void Execute(IServiceProvider serviceProvider)
     public async Task ExecuteAsync(IServiceProvider serviceProvider)
            context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            organizationService = serviceFactory.CreateOrganizationService(context.UserId);
            serviceContext = new OrganizationServiceContext(organizationService);
            tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            contact createContact = new Contact(){
               firstname = "Shayan",
               lastname = "RS"   
           CreateRequest createRequest = new CreateRequest();
           createRequest.Target = createContact;
           CreateResponse createResponse = (CreateResponse);

Plugin Code
Have not written the detail plugin code, but just the syntax as follows

   string tagValue = context.SharedVariables["tag"].ToString();
      if(tagValue == "ApiContact")

this way once the tag is passed from the custom api, and when plugin accesses it, it can be used to skip the plugin execution.