Sample: Work with business process flows


Microsoft Flow is now Power Automate. For more information, see this blog.

This content will be updated to reflect the branding change in the coming days.

This sample demonstrates how to programmatically work with business process flows such as retrieving the business process flow instances for an entity record, retrieving active path for a business process flow instance and its process stages, and changing the active stage. For information about these concepts, see Work with business process flows using code

This sample is available to download from Sample: Work with business process flows.


Before you can run the sample:

  1. Have access to a Common Data Service environment.

  2. Have appropriate privileges on the Lead, Opportunity, and Workflow entities and business process flow definition entity records used in this sample.

  3. Have Visual Studio 2015 or later to run the sample.

  4. Have Internet connection to download the sample project and to restore the NuGet packages used in the sample project.

What this sample does

  1. Creates a sample Lead record. This automatically creates an instance of the "Lead To Opportunity Sales Process" business process flow for the Lead record.

  2. Converts the Lead record to an Opportunity record.

  3. Retrieves the business process flow instances associated with the "Opportunity" record using the RetrieveProcessInstances message. The first record in the returned collection is the active business process flow instance for the opportunity record, which is "Lead To Opportunity Sales Process" in this case.

  4. Retrieves the active path and the process stages for the "Lead To Opportunity Sales Process" instance using the RetrieveActivePath message.

  5. Retrieves the currently active stage for the "Lead To Opportunity Sales Process" instance, and prompts the user whether to move to the next stage. On confirmation to move, sets the next stage in the active path as the active stage for the "Lead To Opportunity Sales Process" instance.

  6. Finally, prompts the user whether to delete the records created during the sample run.

    Here is the output of the sample:

    Sample output

Run the sample

  1. Download the WorkWithBPF Visual Studio sample project, and extract it to a folder on your computer.

  2. Locate the WorkWithBPF.sln file in your extracted folder, and open it in Visual Studio.

  3. The sample project uses NuGet packages that must be restored before running the sample. Ensure that automatic restore of NuGet packages is enabled in Visual Studio. More information: Enabling and disabling NuGet package restore

    Alternatively, select Project > Manage NuGet Packages, and select Restore to manually restore the packages used in the sample.

  4. Press F5 or select Debug > Start Debugging.

  5. If you have not previously run one of the samples before, you’ll need to enter information to run the code, otherwise enter the number for one of the instances you have previously set up.

    Prompt Description
    Enter a Dynamics 365 server name and port [] Type the name of your Dynamics 365 server. The default is Dynamics 365 (online) ( in North America.

    Is this organization provisioned in Microsoft online services (y/n) [n] Type y if this is a Microsoft online services provisioned organization. Otherwise, type n.
    Enter domain\username Type your Microsoft account.
    Enter password Type your password. The characters will show as “*” in the window. Your password is securely saved in the Microsoft Credential Manager for later reuse.
    Specify an organization number (1-n) [1] From the list of organizations shown that you belong to, type the corresponding number. The default is 1, indicating the first organization in the list.
  6. The sample will perform the operations described in What this sample does and may prompt you with additional options.

  7. When the sample is complete, press ENTER to close the console window.


Can you tell us about your documentation language preferences? Take a short survey.

The survey will take about seven minutes. No personal data is collected (privacy statement).