Logo 
Search:

MOSS Forum

Ask Question   UnAnswered
Home » Forum » MOSS       RSS Feeds

Workflows and persisting instance variables

  Date: May 30    Category: MOSS    Views: 1216

I've created a State Machine Workflow that has custom variables that can
be stored within the instance of the Workflow itself. Most of these
variables are used to store custom settings and parameters for the
workflow. The variable values are usually loaded once when the
workflow first starts and then referenced throughout the life of the
workflow during OnTaskChanged events and etc.
Problem:
When I use a global variable defined at the top of the .cs C# code file
that is a Class type (e.g. SPListItem, SPList, *MyCustomClass) the
workflow fails to function normally. The symptom is, when the workflow
is started it runs successfully, then immediately sets it's status to
"Completed". This is not what should happen since the workflow has many
steps before completion and should never complete after first starting.
I dont' think declaring the global variable causes it to behave
strangely, it's when the variable is actually used, such as creating a
new instance or assigning a value to the global variable causes it to
set to "Completed".
I have experimented with different variable types and the only way I can
get the workflow to not auto-complete upon starting is to just not use
any complex types or class types as global variables. If I use simple
strings and int's everything is fine.
The below sample code works:
public sealed partial class Workflow1 : StateMachineWorkflowActivity
{ public Workflow1() { InitializeComponent();
}
private string MyVariable = ""; public
SPWorkflowActivationProperties workflowProperties = new
SPWorkflowActivationProperties();
//**Workflow implementation code here**
}
The below sample code does NOT work, and the workflow sets its status to
"Completed" after running
public sealed partial class Workflow1 : StateMachineWorkflowActivity
{ public Workflow1() { InitializeComponent();
}
public SPListItem MyCustomList = null; public
SPWorkflowActivationProperties workflowProperties = new
SPWorkflowActivationProperties();
//**Workflow implementation code here**
}

One thing I tried which worked was to declare the Class variables as
"static". Such as this:
public static SPListItem MyCustomList = null;
The workflow functioned perfectly, however, using public static is not
desired because the global variables need to be scoped within the
workflow itself and NOT be accessible outside of its own workflow
instance. I tested this and the static variable are accessible across
multiple running workflow instances. I have yet to experiment with
"private static" instead.
What is the correct approach for storing Class variables stored within
each separate workflow instance. Any ideas?

Share: 

 

1 Answer Found

 
Answer #1    Answered On: May 30    

Sorry for the poor text formatting in my original post. The code wasn't very
readable. I ended up working around my solution by using primitive types
(strings) as my instance level (global) variables. I believe the solution I was
striving toward requires that the custom classes be decorated with xml
serialization definitions as well as other settings in code. I did some
research on it and got it to work within a local Visual Studio project. However
the same code would not port to SharePoint. There was more work involved to get
the xml serializers to work, and I found myself getting into a repetitive cycle
of trying lots of various settings permutations to find just the right settings,
with no luck.

Due to the extra time this was taking, I ended up using a completely different
approach. I wrote my own serializer-like routine to handle my custom objects
without actually using any XmlSerializer class references in my code. This
approach involved converting the custom classes into a large XML Document by
manually inspecting each element inside my custom classes, creating an XML doc
which represented the same structure, then storing the entire document as a
single string into a string variable. This works like a charm now, and I've
had no further issues.

This solution is not the cleanest, and requires me to write a lot of code for
every thing I add to the custom classes. But, it does work...

If anyone has any suggestions on other ways to do this, I'd be grateful.

 
Didn't find what you were looking for? Find more on Workflows and persisting instance variables Or get search suggestion and latest updates.