HOW TO: Programmatically set/pass WorkflowContext from a Visual Studio workflow to a custom workflow action in SharePoint 2010

Published 05-01-2019 03:31 PM 98 Views
Microsoft

First published on TECHNET on Sep 25, 2012

This post is a contribution from Himani Sharma, an engineer with the SharePoint Developer Support team.

Scenario:

You have a workflow custom action that you’d like to be used in SharePoint 2010 workflow.  This activity uses Microsoft.SharePoint.WorkflowActions.WorkflowContext to obtain WorkflowContext and hence use the workflow instance specific properties like ItemId, ListId, TaskListGuid, WorkflowInstanceId etc.,

Analysis:

In order to use this workflow custom action in SharePoint designer workflow, we create a custom workflow actions file.  The WorkflowContext parameter expected by the custom action is passed like this:

Excerpt from a sample file workflow actions file:

<Parameters>


<Parameter Name="__Context" Type="Microsoft.SharePoint.WorkflowActions.WorkflowContext,Microsoft.SharePoint.WorkflowActions" Direction="In" />


</Parameters>



But how do we pass the WorkflowContext from within a Visual Studio workflow?



Here are the steps:



1. This is how the workflow context is defined in the workflow custom action class.





#region workflow context





public static DependencyProperty __ContextProperty = DependencyProperty.Register("__Context", typeof(WorkflowContext), typeof(ESPTaskActivity));





public WorkflowContext __Context


{


get { return (WorkflowContext)GetValue(__ContextProperty); }


set { SetValue(__ContextProperty, value); }


}





#endregion



2. This is how you can set or pass the value from a Visual Studio workflow that uses this workflow custom action.






a. Define a public variable within the workflow class file.





public WorkflowContext wfContext { get; set; }


b. Set the value from within OnWorkflowActivated event in the workflow class file. NOTE: As long as you’ve access to SPWorkflowActivationProperties, you can set it from anywhere within the workflow.





private void onWorkflowActivated1_Invoked(object sender, ExternalDataEventArgs e)


{


//Initialize workflowcontext using SPWorkflowActivationProperties


wfContext.Initialize(workflowProperties);





//initialize task properties.


CustomTaskActivity1.__Context = wfContext;


}



You are all set!  Hope this post was helpful.

%3CLINGO-SUB%20id%3D%22lingo-sub-509683%22%20slang%3D%22en-US%22%3EHOW%20TO%3A%20Programmatically%20set%2Fpass%20WorkflowContext%20from%20a%20Visual%20Studio%20workflow%20to%20a%20custom%20workflow%20action%20in%20SharePoint%202010%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-509683%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSTRONG%3E%20First%20published%20on%20TECHNET%20on%20Sep%2025%2C%202012%20%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EThis%20post%20is%20a%20contribution%20from%20Himani%20Sharma%2C%20an%20engineer%20with%20the%20SharePoint%20Developer%20Support%20team.%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%20Scenario%3A%20%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EYou%20have%20a%20workflow%20custom%20action%20that%20you%E2%80%99d%20like%20to%20be%20used%20in%20SharePoint%202010%20workflow.%26nbsp%3B%20This%20activity%20uses%20Microsoft.SharePoint.WorkflowActions.WorkflowContext%20to%20obtain%20WorkflowContext%20and%20hence%20use%20the%20workflow%20instance%20specific%20properties%20like%20ItemId%2C%20ListId%2C%20TaskListGuid%2C%20WorkflowInstanceId%20etc.%2C%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%20Analysis%3A%20%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EIn%20order%20to%20use%20this%20workflow%20custom%20action%20in%20SharePoint%20designer%20workflow%2C%20we%20create%20a%20custom%20workflow%20actions%20file.%26nbsp%3B%20The%20WorkflowContext%20parameter%20expected%20by%20the%20custom%20action%20is%20passed%20like%20this%3A%3C%2FP%3E%0A%3CP%3E%3CEM%3E%20Excerpt%20from%20a%20sample%20file%20workflow%20actions%20file%3A%20%3C%2FEM%3E%3C%2FP%3E%0A%3CDIV%20id%3D%22codeSnippetWrapper%22%3E%0A%3CDIV%20id%3D%22codeSnippet%22%3E%3CPARAMETERS%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CPARAMETER%20name%3D%22%26quot%3B__Context%26quot%3B%22%20type%3D%22%26quot%3BMicrosoft.SharePoint.WorkflowActions.WorkflowContext%2CMicrosoft.SharePoint.WorkflowActions%26quot%3B%22%20direction%3D%22%26quot%3BIn%26quot%3B%22%3E%3C%2FPARAMETER%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FPARAMETERS%3E%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3EBut%20how%20do%20we%20pass%20the%20WorkflowContext%20from%20within%20a%20Visual%20Studio%20workflow%3F%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%20Here%20are%20the%20steps%3A%20%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3E1.%20This%20is%20how%20the%20workflow%20context%20is%20defined%20in%20the%20workflow%20custom%20action%20class.%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CDIV%20id%3D%22codeSnippetWrapper%22%3E%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippet%22%3E%3CBR%20%2F%3E%23region%20workflow%20context%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3Epublic%20static%20DependencyProperty%20__ContextProperty%20%3D%20DependencyProperty.Register(%22__Context%22%2C%20typeof(WorkflowContext)%2C%20typeof(ESPTaskActivity))%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3Epublic%20WorkflowContext%20__Context%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3Eget%20%7B%20return%20(WorkflowContext)GetValue(__ContextProperty)%3B%20%7D%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3Eset%20%7B%20SetValue(__ContextProperty%2C%20value)%3B%20%7D%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%23endregion%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3E2.%20This%20is%20how%20you%20can%20set%20or%20pass%20the%20value%20from%20a%20Visual%20Studio%20workflow%20that%20uses%20this%20workflow%20custom%20action.%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F111411iEA71AA5C9A405E71%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3Ea.%20Define%20a%20public%20variable%20within%20the%20workflow%20class%20file.%3C%2FP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippetWrapper%22%3E%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippet%22%3E%3CBR%20%2F%3Epublic%20WorkflowContext%20wfContext%20%7B%20get%3B%20set%3B%20%7D%3C%2FDIV%3E%0A%3C%2FDIV%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CP%3Eb.%20Set%20the%20value%20from%20within%20OnWorkflowActivated%20event%20in%20the%20workflow%20class%20file.%20NOTE%3A%20As%20long%20as%20you%E2%80%99ve%20access%20to%20SPWorkflowActivationProperties%2C%20you%20can%20set%20it%20from%20anywhere%20within%20the%20workflow.%3C%2FP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippetWrapper%22%3E%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippet%22%3E%3CBR%20%2F%3Eprivate%20void%20onWorkflowActivated1_Invoked(object%20sender%2C%20ExternalDataEventArgs%20e)%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%2F%2FInitialize%20workflowcontext%20using%20SPWorkflowActivationProperties%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EwfContext.Initialize(workflowProperties)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%2F%2Finitialize%20task%20properties.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3ECustomTaskActivity1.__Context%20%3D%20wfContext%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%7D%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FBLOCKQUOTE%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3EYou%20are%20all%20set!%26nbsp%3B%20Hope%20this%20post%20was%20helpful.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-509683%22%20slang%3D%22en-US%22%3E%3CP%3EFirst%20published%20on%20TECHNET%20on%20Sep%2025%2C%202012%20This%20post%20is%20a%20contribution%20from%20Himani%20Sharma%2C%20an%20engineer%20with%20the%20SharePoint%20Developer%20Support%20team.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-509683%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EBest%20Practices%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Co-Authors
Version history
Last update:
‎Apr 30 2021 12:27 PM
Updated by: