Recently, the company asked me this cainiao to do a Sharepoint workflow. I also know a little about workflows. I have learned a lot about workflows before. Now I am responsible for workflow projects, so now I am desperate to learn. SharePoint 2010 workflow is based on Workflow 3.5. So I am still learning 3.5. Ah, people are already in touch with 4.0. No nonsense. Learn tracking today.
1: Overview of tracking
Some events are triggered when a workflow is executed. The tracking service can capture these events during workflow execution, and view the instance status of the workflow during execution.
These events
2: simple example
Let's take a simple example to see how this tracking works. The Workflow Foundation provides a system for us. workflow. runtime. tracking. sqltrackingservice class, which provides the tracking service for storing SQL database trace information. This instantiation class needs to pass a database connection string. This database connection is the SQL Server database address where we need to store tracking information. Create an SQL tracking database first.
2-1: Create an SQL tracking database
Create a database first:
Create Database wftracking
After creating the database wftracking, we can find the following two database scripts. And execute these two database scripts.
% Windir % \ microsoft. Net \ framework \ V3.0 \ Windows Workflow Foundation \ SQL \ <language> \ tracking_schema. SQL
% Windir % \ microsoft. Net \ framework \ V3.0 \ Windows Workflow Foundation \ SQL \ <language> \ tracking_logic. SQL.
2-2:
1: Create a 3.5 ordered workflow console applicationProgram.
We do not add any activity
2: Open the program. CS file and reference the namespace system. workflow. runtime. Tracking.
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading; using system. workflow. runtime; using system. workflow. runtime. hosting; using system. workflow. runtime. tracking; namespace trackingconsoleapp {class program {/// <summary> /// Tracking Service database link /// </Summary> static string strconn = "Initial catalog = wftracking; data Source = localhost; Integrated Security = Sspi; "; static void main (string [] ARGs) {using (workflowruntime = new workflowruntime () {autoresetevent waithandle = new autoresetevent (false); workflowruntime. workflowcompleted + = delegate (Object sender, workflowcompletedeventargs e) {waithandle. set () ;}; workflowruntime. workflowterminated + = delegate (Object sender, workflowterminatedeventargs e) {console. writeline (E. exceptio N. message); waithandle. set () ;}; // register the SQL tracking service workflowruntime. addservice (New sqltrackingservice (strconn); workflowinstance instance = workflowruntime. createworkflow (typeof (trackingconsoleapp. workflow1); instance. start (); waithandle. waitone (); workflowruntime. stopruntime (); getinstancetrackingevents (instance. instanceid); getactivitytrackingevents (instance. instanceid) ;}} static void getinstance Trackingevents (guid instanceid) {sqltrackingquery = new sqltrackingquery (strconn); // obtain workflow instance metadata; sqltrackingquery. trygetworkflow (instanceid, out of memory); If (sqltrackingworkflowinstance! = NULL) {console. writeline ("\ n instance-level event: \ n"); foreach (workflowtrackingrecord in sqltrackingworkflowinstance. workflowevents) {console. writeline ("trackingworkflowevent: {0} eventdatetime: {1}", workflowtrackingrecord. trackingworkflowevent, workflowtrackingrecord. eventdatetime) ;}} static void getactivitytrackingevents (guid instanceid) {sqltrackingquery = N EW sqltrackingquery (strconn); sqltrackingworkflowinstance; sqltrackingquery. trygetworkflow (instanceid, out sqltrackingworkflowinstance); If (sqltrackingworkflowinstance! = NULL) {console. writeline ("\ n Activity-level event: \ n"); foreach (activitytrackingrecord in sqltrackingworkflowinstance. activityevents) {console. writeline ("executionstatus: {0} eventdatetime: {1} qualifiedname: {2}", activitytrackingrecord. executionstatus, activitytrackingrecord. eventdatetime, activitytrackingrecord. qualifiedname );}}}}}
Note:
Sqltrackingquery can obtain certain types of tracking data in the tracking database. The sqltrackingquery. trygetworkflow method attempts to obtain the trace data of the workflow instance in the sqldatabase.
You can also configure the service in APP. config:
<? XML version = "1.0" encoding = "UTF-8" ?>
< Configuration >
< Section Name = "Workflowruntime"
Type = "System. workflow. runtime. configuration. workflowruntimesection,
System. workflow. runtime, version = 3.0.255.0.0, culture = neutral,
Publickeytoken = 31bf3856ad364e35" />
< Workflowruntime >
< Commonparameters >
<! -- Add parameters common to all services -->
< Add Name = "Connectionstring"
Value = "Initial catalog = wftracking; Data Source = localhost; Integrated Security = sspi ;" />
</ Commonparameters >
< Services >
<! -- Add core services here -->
< Add Type = "System. workflow. runtime. Tracking. sqltrackingservice,
System. workflow. runtime, version = 3.0.255.0.0, culture = neutral,
Publickeytoken = 31bf3856ad364e35" />
</ Services >
</ Workflowruntime >
</ Configuration >
Run this program. Here we can see at which time and what events are executed by the workflow. I can see that the time seems to be different from the time on my computer. Well, this is because the time format of the database is GMT. GMT is eight hours later than that in China. This solves the problem of server time difference in different time zones.
2-3: extend after a simple example ......
We modify the workflow and add an activity to the workflow.
Private void codeactivityappsexecutecode (Object sender, eventargs e) {console. writeline ("Run codeactivity1");} run the program again. Cup, error. The problem is obvious. We did not find codeactivity1 we just added.
After registering sqltrackingservices, when workflowruntime loads a workflow, it first searches for the Workflow Information in the tracking database. If it does not, it is saved to the database. If it does, it does not update the Workflow Information of the database. The solution is to delete the original information in the database or modify the workflow class name. The most common method is to modify the workflow version, that is, to modify the assembly version.
2-4: User tracking events
User track point is added to a workflow or custom activity. How to add a user trail? You only need to use the trackdata method to save the tracking service records in the data.
Private Void 1st code_executecode ( Object Sender, eventargs E)
{
Console. writeline ( " 1st code_executecodes " );
This . Trackdata ( " 1st codes: " + Datetime. Now. tostring ());
}
Get User tracking:
Private Static Void Getusertrackingrecords (system. guid instanceid)
{
Sqltrackingquery = New Sqltrackingquery (strconn );
Sqltrackingworkflowinstance wfinstance;
Query. trygetworkflow (instanceid, Out Wfinstance );
Foreach (Usertrackingrecord usertr In Wfinstance. userevents)
{
Console. writeline ( " ========================================== " );
Console. writeline ( " Activity: " + Usertr. qualifiedname );
Console. writeline ( " Activity Type: " + Usertr. activitytype. tostring ());
Console. writeline ( " Context ID: " + Usertr. contextguid. tostring ());
Console. writeline ( " Time: " + Usertr. eventdatetime. tostring ());
Console. writeline ( " Content: " + Usertr. userdata. tostring ());
Console. writeline ( " Eventorder field value: " + Usertr. eventorder. tostring ());
Console. writeline ( " Parent context ID: " + Usertr. parentcontextguid. tostring ());
Try {Console. writeline ( " Key-value pairs: " + Usertr. userdatakey. tostring ());}
Catch {Console. writeline ( " This non-key-value pair! " );}
Console. writeline ();
}
}