Use the IOrganization web service in a custom workflow activity

 

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

To call Microsoft Dynamics 365 or Microsoft Dynamics 365 (online) organization web service methods from within a custom workflow activity, you must first obtain a reference to the web service. This is described in the following procedure and sample code.

  1. Get a reference to IOrganizationServiceFactory.

  2. Use the IOrganizationServiceFactory.CreateOrganizationService method to create an instance of IOrganizationService.

  3. Use the IOrganizationService instance to call the supported methods.

Example

The following sample shows how to get the IOrganizationService from within the Execute method of your custom workflow activity.

protected override void Execute(CodeActivityContext executionContext)
{
   // Get the context service.
   IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
   IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();

   // Use the context service to create an instance of IOrganizationService.
   IOrganizationService _orgService = serviceFactory.CreateOrganizationService(context.InitiatingUserId);

   // Use the service reference to call web methods.
   _orgService.Execute(…);
}

See Also

IWorkflowContext
IOrganizationServiceFactory
IOrganizationService
Custom workflow activities (workflow assemblies)
Register and use a custom workflow activity assembly
Sample: Create a custom workflow activity
Use the Organization Service to read and write data or metadata
Organization service methods

Microsoft Dynamics 365

© 2016 Microsoft. All rights reserved. Copyright