Fire a Workflow in Dynamics CRM 2011 using C#

The following is a code sample that calls two methods: GetWorkflowId and ExecuteWorkFlow to run Workflow Programatically either C# or Plug-In :

One important thing to note is that the workflow we are executing must be marked as an on-demand or child workflow; otherwise you will receive an error below.

\”Workflow must be marked as On Demand or Child Workflow\”

 public void Execute(IServiceProvider serviceProvider)
            // Get Workflow or Process Id
            // \”Test_WF\”  = Workflow Name
            // \”workflow\” = Process Schema name
            // \”incident\” = Entity Name on which workflow will run
            // \”false\”    = either workflow must be \’ondemand\’ or \’child workflow\’
            //  1         = indicate Type of workflow 1-Activate, 2- Draft

            Guid processId = GetProcessId(service, \”workflow\”, \”Test_WF\”,\”incident\”,false,1).Id;

            // Execute Workflow or Process
            ExecuteWorkFlow(service, processId, new Guid(\”BF2184E0-51D4-E311-A7B2-7427EA0AF460\”));


 private Entity GetProcessId(IOrganizationService service,string Process,string WorkflowName,string EntityName,bool IsOnDemand,int Type)

                        //Internal method used to search for entity records
                        QueryExpression query = new QueryExpression(Process);

                        query.ColumnSet = new ColumnSet(true);

                        query.Criteria.AddCondition(new ConditionExpression(\”name\”, ConditionOperator.Equal, WorkflowName));

                        query.Criteria.AddCondition(new ConditionExpression(\”type\”, ConditionOperator.Equal, Type));

                        query.Criteria.AddCondition(new ConditionExpression(\”primaryentity\”, ConditionOperator.Equal, EntityName));

                        query.Criteria.AddCondition(new ConditionExpression(\”ondemand\”, ConditionOperator.Equal, IsOnDemand));



                        EntityCollection response = service.RetrieveMultiple(query);

                        if (response != null && response.Entities.Count > 0)

                        return response.Entities[0];

                        else { return null; }


                        catch (Exception ex)


                        //Do error handling

                        throw ex;



private void ExecuteWorkFlow(IOrganizationService service, Guid workFlowId, Guid entityId)

                // Create an ExecuteWorkflow request.

                ExecuteWorkflowRequest request = new ExecuteWorkflowRequest()

                    WorkflowId = workFlowId,

                    EntityId = entityId,


                // Execute the workflow.

                ExecuteWorkflowResponse response =



            catch (Exception ex)
                throw new InvalidPluginExecutionException();


Cheerssss 🙂

Published by arpitpowerguide

My name is Arpit Shrivastava, who is a Microsoft MVP in the Business Applications category. I am a Microsoft Dynamics 365 and Power Platform enthusiast person who is having a passion for researching and learning new things and acquiring immense knowledge. I am providing consistent help, support, and sharing my knowledge through various Social Media Channels along with my Personal Blog, Microsoft Community, conducting online training and attending various 365 Saturday Events worldwide and sharing the best Solutions to the readers helping them achieve their goals and objectives in Customer Relationship Space.

One thought on “Fire a Workflow in Dynamics CRM 2011 using C#

  1. Special thanks to ( for exposing my cheating husband. Right with me i got a lot of evidences and proofs that shows that my husband is a fuck boy and as well a cheater ranging from his text messages, call logs, whats-app messages, deleted messages and many more, All thanks to (, if not for him i will never know what has been going on for a long time. Contact him now and thank me later.


Leave a Reply to Sophia Mia Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: