0
votes

In the past I have developed document workflows using SharePoint Designer and created 'Global Vairables.' These are variables that are global across a workflow instance as far as I am aware.

How would I create equivalent global variables in a Visual Studio Workflow project? My first idea was to create a set of fields like this in a module:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Field ID="{2FE15855-3CAB-44A6-AB29-1600204FCA20}" Name="TechRev1"
         MaxLength="255" DisplayName="Technical Reviewer 1" Description=""
         Direction="None" Type="User" Overwrite="TRUE" />
  <Field ID="{517B22A5-1B89-4C24-82BE-3D4FD99645BC}" Name="TechRev2"
         MaxLength="255" DisplayName="Technical Reviewer 2" Description=""
         Direction="None" Type="User" Overwrite="TRUE"/>
<Elements/>

Is this the right approach and how would I access any global variables from an ASPX form?

1
For a VS workflow you can write code, so it's the same as asking how to have global state in C#. Your best bet is to just store the data in a list somewhere if it's an option, especially if you plan to access it from an ASPX page.Servy
Thank you Servy. I don't believe Sharepoint Designer uses a list for global state retention (I definitely could be wrong!). Can you create one 'hidden' list instance per workflow instance? I basically want a global memory area that persists over server restart and can only be read by the workflow that created it.Andrew Bennett
I have no idea if designer workflows use a list for global state internally or not, I know that I frequently explicitly uses lists myself for storing state that I use in my workflows (in both VS and designer). You likely don't want one list per instance, you want one list and an item in that list per instance. As for making it hidden, you can break inheritance and remove everyone's permissions so that only site collection admins could modify it.Servy

1 Answers

0
votes

Having developed my workflow in Visual Studio I have come to conclusion you should use a combination of SharePoint lists and class scoped variables to retain global state in a workflow.

You should use lists when..

  • Data needs to be accessed across many different classes in a workflow

You should use class scoped variables when..

  • The data is only needed in one class

  • The data is of a type can be serialised (primitive types, a type marked with [Serializable]). I have one such type in my solution:

    [Serializable] public class ReviewerBag { public Dictionary Reviewers { get; set; } public string Moderator { get; set; }

    public ReviewerBag()
    {
        Reviewers = new Dictionary<string,Reviewer>();
    }
    ...
    

    }

  • If the data is needed between classes in a workflow you can still use variables but beware that each time the variable is declared in a class it will be serialised independently when the workflow becomes idle (e.g. when the workflow waits for a user to complete a task). The variables become out of sync as they are deserialised independently on workflow resume. Moral of the story is use lists to store the data in this scenario!