Calling / Activate a workflow(CRM 4.0) from c# code / SDK RRS feed

  • Question

  • Calling / Activate  a workflow(CRM 4.0) from c# code  / SDK

    Hi all,
    I want to call or active a workflow from the SDK by c# code, I didn't find any posts about it.


    Tuesday, December 2, 2008 8:39 AM


All replies

  • from SDK:


    Code Snippet

    // Set up the CRM Service.
    CrmAuthenticationToken token = new CrmAuthenticationToken();
    // You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
    token.AuthenticationType = 0;
    token.OrganizationName = "AdventureWorksCycle";
    CrmService service = new CrmService();
    service.Url = "http://<servername>:<port>/mscrmservices/2007/crmservice.asmx";
    service.CrmAuthenticationTokenValue = token;
    service.Credentials = System.Net.CredentialCache.DefaultCredentials;

    // Create an ExecuteWorkflow request.
    ExecuteWorkflowRequest request = new ExecuteWorkflowRequest();

    //Assign the ID of the workflow you want to execute to the request.        
    request.WorkflowId = new Guid("b050f053-6968-dc11-bb3a-0003ffbad37a");

    //Assign the ID of the entity to execute the workflow on to the request.
    request.EntityId = new Guid("1DCDEE97-35BB-44BE-8353-58BC36592656");

    // Execute the workflow.
    ExecuteWorkflowResponse response = (ExecuteWorkflowResponse)service.Execute(request);





    Tuesday, December 2, 2008 9:07 AM
  • Thank you very much about your post…
    My follow-up question will be:  how can I pass an InputParameters to the workflow (if it is a custom activity), is it done with the OptionalParameters ?


    Tuesday, December 2, 2008 9:45 AM
  • By the way, I very filmier with yours blog, and like it very much, It's a great assist in CRM development.

    Tuesday, December 2, 2008 9:52 AM
  • Tuesday, December 2, 2008 12:19 PM
  • Dear Jim,

    The link provided unfortunatly doesn't work anymore.
    I would be very much interested in these discussions as I'm currently working on a "Share record only after approval" customization whereby I'm trying to call a workflow from an c# MS Dynamics 4 CRM plugin. From your two responses I assume it is possible to do so and some code to get me started would be great.

    For the record, my simplified approach is as follows:

    1. Create(d) a plugin which catches the "GrantAccess" event
    2. Check if BU of user to share with >= BU of entity record to share. If true share record. If not call SharingApproval workflow with the parameters, AccessMask, PrincipalID(user to share with), TargetId (record to share), Target Name (entity), maybe some other parameters.
    3. SharingApproval workflow creates Sharing Approval record for persistance sake
    4. Notifies the approver that there is a pending approval item
    5. Approver marks the approval record in dynamics as approved
    6. workflow no shares the record as requested. (Can be the same workflow or a second one which gets trigger through the status field change, probably better.

    Would be interested in hearing your opinion concerning this approach.
    (Background: Work for a financial institution as sharing records is sensitive, currently it is basically disable (set to BU only which is b..s.. as they can see the record anyway...)

    Kind Regards from Singapore,


    Tuesday, September 8, 2009 8:13 AM
  • Hi Jim,

    I've been executing workflows from within code using the

    ExecuteWorkflowResponse response = (ExecuteWorkflowResponse)service.Execute(request);

    The first few execute in under a second, however after the first few they do take up to as much as 20 seconds each!

    have you any suggestions, dispose of a certain object. set it as null, test for a scenario before firing the next workflow?



    Monday, July 12, 2010 12:51 PM
  • Hi Ian I dont know the exact scenario in which you need to invoke the workflow. However i have done something similar but i use a windows service to schedule the workflow. You can read more about this on my blog.






    Thursday, July 29, 2010 10:38 PM
  • Did you find a solution to passing parameters to workflow through sdk.
    Thursday, July 29, 2010 11:07 PM
  • Any ideas how to pass in parameters to a workflow from code? Thanks,
    Saturday, November 13, 2010 4:04 AM
  • There is no way for a workflow to accept parameters, whether through code or when invoked by the CRM application. What are you trying to achieve ?

    Microsoft CRM MVP - http://mscrmuk.blogspot.com  http://www.excitation.co.uk
    Saturday, November 13, 2010 11:39 AM