Sample: Work with business process flows


Applies To: Dynamics 365 (online), Dynamics 365 (on-premises)

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 Model business process flows

This sample is for December 2016 update for Dynamics 365 (online and on-premises), and is available to download from Sample: Work with business process flows.

In This Topic


What this sample does

Run the sample


Before you can run the sample:

  1. Have access to a December 2016 update for Dynamics 365 (online and on-premises) organization.

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

  3. Visual Studio 2013, Microsoft 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 WorkWithBPFVisual 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 Microsoft Dynamics 365 managed code samples before, you’ll need to enter information to run the code, otherwise enter the number for one of the Dynamics 365 servers you have previously set up.



    Enter a Dynamics 365 server name and port []

    Type the name of your Microsoft Dynamics 365 server. The default is Microsoft 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.

See Also

Model business process flows

Microsoft Dynamics 365

© 2017 Microsoft. All rights reserved. Copyright2