Often times when implementing a workflow, we as designers run into a situation where we need a variable to persist between each workflow. Sometimes it’s to determine if a specific process has been completed previously or to count how many times the workflow has run. When we run into these situations, using a workflow variable won’t do since the workflow variables are instantiated anew each time the workflow executes.
To solve this we have to serialize the variable in the only way that we have access to: List Fields. These fields can be set and used as de facto variables. The biggest issue we face using fields in this manner is that the fields we would prefer to keep hidden will appear on the new, edit, and view forms in SharePoint. However, in this article I’ll demonstrate an easy way to eliminate this issue as well.
First, if it is not already, turn on Content Types for the list you’ll be working with.
Next, create a field to store the variable you want to store.
Before you click create, make sure the two checkboxes at the bottom of the screen are checked.
Without checking these boxes, the field will appear on the forms for items of this content type. However, they will still be writable, and even readable from a view if you add the field. This is exactly the behavior we want from our “variable” field.
Now write to the field as you would any item, using the Update Item action in SharePoint Designer.
It is also a good idea to think about whether or not you will ever be in a position to read from the field before data are written to it. If this is the case, and even if it isn’t, a good practice would be to check if the field is empty before reading from it to prevent strange error cases.
Once you've done this, you now have a field which will persist between runs of the workflow but can still be seen in views if the content is added. This is also important to know because this is not a place to store sensitive information, only information not relevant to the user.
The content cannot be written to from the New Item form.
However, it can be seen in a view if configured properly. This is useful for debugging the values stored in these fields.
Think of the value of implementing persistent variables in other cases:
- Allowing for multiple Contract Generations, or restrict it to a single time only with Integer and Boolean variables.
- Restrict the execution of a Reminder Workflow if conditions are met with a Boolean variable.
- Set a programmatically Created Site's new URL in a site tracking list, without letting a user change it inadvertently. Remember, these variables can still be seen by users in views!
If you'd like to learn more about DMC's Microsoft Consulting Services, click here, or if you'd like to work with DMC to implement persistent variables in your SharePoint environment, please contact us.