Thursday, January 8, 2009

Understanding Workflow Tracking Service - Part 3

Hi All,

Here i am back with tracking series for workflow.

Before continue here, i would recommend to go and read previous two parts.

Understanding Workflow Tracking Service - Part 1

Understanding Workflow Tracking Service - Part 2

Ok, by taking the same database and tables and workflow in our hand, run the workflow once again so that we have two ActivityInstances in table and we have two workflowinstances in our database.

Then after running the workflow, just validate again by firing the query in our “WorkflowTracing”database .

Select * from ActivityInstance

Select * from WorkflowInstance

So that you get a result similer to this.



As I have also described you that one tool which we can use to track the tracking service is the WorkflowMonitor tool. But here first we will explore two classes that will be used to extract the information from tracking database.

Ok, let’s write a code inside our tracker console application. But before doing this, do not forget to sign the assembly as strong name otherwise you will not get the Activity and workflow Events. After signing your assembly with strong name only execute this code.

Go to your program.cs and after waitHandle.WaitOne(); write the following code.

#region "Fetch the ActivityEvents"

SqlTrackingQuery objQuery = new SqlTrackingQuery(strConnection);
SqlTrackingWorkflowInstance objInstance = null;

objQuery.TryGetWorkflow(instance.InstanceId, out objInstance);

if (objInstance != null)
{
foreach (ActivityTrackingRecord objRecord in objInstance.ActivityEvents)
{
Console.WriteLine(" {0} :: {1} :: {2} ", objRecord.ExecutionStatus,
objRecord.EventDateTime, objRecord.QualifiedName);
}

Console.Read();
}

#endregion

Here as you can see we have first taken the SqlTrackingQuery object and initialized with the tracking services and then we got the SqlTrackingTrackingWorkflowInstance and then once we have it , we got the ActivityEvents and then iterate through its collection and accessed different properties. You can try and explore more and more properties for the same. For example only I have used three of them and displayed them on console.

See figure below.



Same way write down the below code next to the ActivityEvents iterations in the same way. This is for the WorkflowEvents to track.

#region "Fetch the ActivityEvents"

SqlTrackingQuery objQuery = new SqlTrackingQuery(strConnection);
SqlTrackingWorkflowInstance objInstance = null;

objQuery.TryGetWorkflow(instance.InstanceId, out objInstance);

if (objInstance != null)
{
foreach (WorkflowTrackingRecord objRecord in objInstance.WorkflowEvents)
{
Console.WriteLine(" {0} :: {1} ",
objRecord.EventDateTime, objRecord.EventOrder);
}

Console.Read();
}

#endregion

This way we have fetched the built in events that are trapped automatically by Workflow Tracking service but what if we want to track our own custom events to the database.

Because many a times we want to capture our own tracing, like we can say CodeActivity1 has started. Code activity1 is in process, this step done and code activity ended, this was the user, this happened. Anything that we want to track we can track it if we write our own messages.

Let’s have a look on them.

Activity supports a method called TraceData. It has two overloaded method, one takes the object as a parameter and other takes string as an input for key and an object as a value to store.

So modify our BeforeDelay and AfterDelay activity like this and then run the program again.



Once you are done with the execution of the program, open the database and look for UserEvent table, you will find your own entries that you just did in above code.



This is one way to get the tracked workflow events and activity events from the database. Once you have the values you can define your custom application to display those values.

Next we will see the Workflow Monitor tool to see how this same process can also be seen in using this Monitor tool.

I will continue this series in my next article.

No comments:




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com