Click here to Skip to main content
14,270,306 members

Document approval workflow system

Rate this:
4.06 (25 votes)
Please Sign up or sign in to vote.
4.06 (25 votes)
24 Mar 2007CPOL
Generic, full cycle of document approval workflow system, using Windows Workflow Foundation (WF).

Screenshot - wwf_document.gif

The purpose of the article

Demonstrate, a quick view of workflow foundation, and highlight the basic knowledge that

we should know before create first useful workflow application,

and highlight a design guide lines to create reusable workflow applications.

Finally, to taste the workflow through a sample project that is generic and useful for any

document workflow system, from the client application to administration application using


Workflow foundation, in brief:

We can see the workflow foundation, simply as a collection of activities that can work

together through conditions and rules for specific purpose.

Workflow foundation is not if else blocks with loop blocks, and if it was implemented as

this simple algorithm, it will cost us a lot for no benefits. The bottom line of using

workflow foundation is, simplifying a

complex algorithm to simple visual editable, maintainable workflow,

Replaced my old design, with workflow:

I used the command patterns a lot in my work, as an encapsulation of the request in

single object called command, that is initialized with parameters ,

then execute method will be invoked to retrieve the result, or to do the specific tasks,

But I always have a problem in sharing the properties, information and resources, or the

result(s) of the other commands,

Some commands were waiting until other commands finished,and others will begins from

the ending point of others,

To solve this problem I always use a mediator which in most cases is CommandManager

class that has a collection of a commands, and knows the order of execution of all

commands,and share all parameters, through the container scope, and each command has

a reference to the command manager,

so it can access all the properties of all other command very easy, or setting any

property for other command or the out client application.


to make the design good, I always inherit all the commands from CommandBase

abstracted class to give me the contract of all commands,so the CommandManager can

execute and initialize all childs commands.

The funny thing is,

that each time I try to create the command objects, I found myself made the same

generic pattern,and wondering if there any way to visualize these library for better use?

Once I saw the sequential workflow activities first time, I said "Aha"

That is exactly what I want, commands (Activities ), has a container command manager

(SequentialWorkflowActivity),and all commands has the same interface or base class

(Activity), and all commands can get the data through contacting the command manager

Sequential workflow

Finally visualizing every thing for quick development, and for better maintainability it is a

great stuff.

What impressed me in workflow foundation?

Sure every thing in workflow foundation impressed me, But the Idea of Adding service(s)

in runtime, then using this service in the workflow through referencing the service

using the public method GetService of WorkflowRuntime object, or through special

activities like CallExternalMethodActivity, HandleExternalEventActivity, it is

fantastic Idea,

because simply you can separate all the abstracted classes and interfaces to a

separate assembly, then make the workflow and the core application reference that

assembly, so you increased the level of reusability of workflow, because the workflow was

coded against the interface(s) but you can in runtime register any concrete class, that

implement this interface,

you can integrate the wonderful Provider pattern that introduced in .Net 2 with the

wonderful workflow foundation through registering any service in web.config, in workflow

section (this topic is beyond this simple project) Once you understand how to use, the

custom service with in the workflow You will understand all

other services, including "out of box" services like WorkflowPersistenceService,

WorkflowSchedulerService, TrackingService,..

Basics that you should know,

Before developing, the first practical application,

as I mentioned, using workflow as a bunch of "if else" statements, is not good idea at

all, and you should know the minimum information about workflow You should know

• How to pass parameters to workflow and to retrieve parameters, after the workflow


• How to create a custom activity, to work for specific task.

• How to use custom service, and consuming this service inside the workflow.

• How to pass data from host application to workflow or from workflow to application

through workflow processing. I think that is the minimum that you should know to create

a useful workflow application For advanced work with the workflow you may need to now,

• Using WorkflowPersistenceService, and Load and Unload to database,

• Using Transaction scope for data consistency.

• Create fault handler, in case of errors happened inside the workflow. Sue that is a lot to

mention about, the workflow specially if you, use the workflow in Asp.NET applications or

in webservice,

Or you think to update simple windows form after completing the workflow

or in specific events. But that is not the purpose of this article.

Design guide lines to work with workflow foundation

I tried to apply the workflow with objects that I already wrote before using my skills In

object oriented programming, using the workflow but I found it is quite difficult to use the

workflow, without any preparation to my code, exactly like first time I tried to use Unit

test to test "not testable " applications

I found that I have to make my code testable through dependency injection and

constructor injection and mockobjects,

Now To use the workflow you have to change your way in developing, So instead using

100% object oriented programming you should move object behaviors from concrete

classes to services,

so your code will be more service oriented

Steps for preparing your code, before using workflow:

Basic 3 steps

1) Separate all object behaviors to Services.

2) Extract the Interface for these services (user refactoring in voisual stodio 5) , so all

your services should implement services interfaces

3) Separate these interfaces to separate assembly

Consider this simple example

Assume you always use a class called

Customer, has these properties

ID, Name, Address,

and has these methods

Add(), Update(), Delete(), Attach(), Register(), Unregister(),..

To use the workflow , you should separate this object state and Identitylike like ID, Name,

Address And move the behavior (public methods, events,..) to CustomerService that

Implement ICustomerService.

In this case you can use the workflow very easy with your prepared code.

In source sample,

you will see the Document Object is inherited from the IDocumentService, and both of

them used for describing the whole document process

The Sample project in Brief

The Solution sample is 3 basic projects and other 2 projects for common objects and


The Idea of the Application is

The user will create a document with his client application,

the user still can update his document as long as is in Open sate.

Then the user will kick the document to workflow, then depending on the document

properties , the workflow will choose the Administrator (s), that is delegated to approve

this document, and will save the data to database and will wait until the Administrator to

complete the cycle.

The Administrator after the login screen (sample will work without password) will see all

documents that are waiting for him,

and then he will approve or Reject the document,

so the document will be flagged as approved or to open state again to the user that can

update the document or delete it or to re submit again to the workflow,

Client Application Sample

Image 2

Create document inside the main form

private void btnCreateDocument_Click(object sender, EventArgs e)
         if (ValidateForm())
             Document _document = new Document();
             _document.Category = (Category)this.cmbCategory.SelectedItem;
             _document.Title = this.txtTitle.Text.Trim();
             _document.Description = this.txtDescription.Text.Trim();
             _document.Depositor = User.CurrentUser.UserName;
         else {
             MessageBox.Show("Please complete, the form's fields", "Validation, Error", MessageBoxButtons.OK
                        , MessageBoxIcon.Error);

IDocument services class diagram
Image 3

document Database Diagram
Image 4

Document services class diagram

Image 5

   public class DocumentService : IDocumentService
       public Document GetDocumentByID(int id)
           return DocumentDalc.GetDocumentByID(id);

       public Documents GetCreatedDocuments(string depositor, Status status)
           return DocumentDalc.GetAllCreatedDocuments(depositor, status);

       public Documents GetCreatedDocuments(string depositor)
           return DocumentDalc.GetAllCreatedDocuments(depositor);

       public Documents GetAllNeedToApproveDocuments(string userName)
           return DocumentDalc.GetAllNeedToApproveDocuments(userName);

       public DocumentWorkflowItem GetWorkflowItem(Document document)
           DocumentWorkflowItem wfItem = new DocumentWorkflowItem(document);
           return wfItem;

       #region IDocumentService Members

       public void CreateDocument(Document document)
           if (this.Created != null) Created.Invoke(this, new DocumentEventArguments(document));

       public void UpdateDocument(Document document)
           if (this.Updated != null) Updated.Invoke(this, new DocumentEventArguments(document));


       public void ArchiveDocument(Document document)
           document.Status = Status.Archived;
           if (this.Archived != null) Archived.Invoke(this, new DocumentEventArguments(document));


       public void DeleteDocument(Document document)
           document.Status = Status.Deleted;
           if (this.Deleted != null) Deleted.Invoke(this, new DocumentEventArguments(document));


       public void CreateInWorkflowDocumentForUser(Document document, string approvalUser, Guid workflowID)
           DocumentDalc.CreateInWorkflowDocumentRecordForUser(document, workflowID, approvalUser);
           if (this.SentToWorkflow != null) SentToWorkflow.Invoke(this, new DocumentEventArguments(document));


       public void CreateInWorkflowDocumentForGroup(Document document, string approvalGroup, Guid workflowID)
           DocumentDalc.CreateInWorkflowDocumentRecordForGroup(document, workflowID, approvalGroup);
           if (this.SentToWorkflow != null) SentToWorkflow.Invoke(this, new DocumentEventArguments(document));


       public void ApproveDocumentWorkflow(Document document, string approvalUser, Guid workflowID)
           DocumentDalc.CreateApprovedDocumentRecordForUser(document, workflowID, approvalUser);
           if (this.Approved != null) Approved.Invoke(this, new DocumentEventArguments(document));


       public void RejectDocumentWorkflow(Document document, string approvalUser, Guid workflowID)
           DocumentDalc.CreateRejectedDocumentRecordForUser(document, workflowID, approvalUser);
           if (this.Rejected != null) Rejected.Invoke(this, new DocumentEventArguments(document));


       #region IDocumentService Members

       public event EventHandler<externalexternaldocumenteventargument __designer:dtid="1688849860263990" /> RaiseApproveDocumentWorkflowEvent;
       public event EventHandler<externalexternaldocumenteventargument /> RaiseRejectDocumentWorkflowEvent;

       public void RaiseApproveDocumentWorkflow(DocumentWorkflowItem wfItem)
           ExternalExternalDocumentEventArgument arg = new ExternalExternalDocumentEventArgument(wfItem.WorkflowID, wfItem.Document, User.CurrentUser);
           if (RaiseApproveDocumentWorkflowEvent != null)
               RaiseApproveDocumentWorkflowEvent.Invoke(this, arg);


       public void RaiseRejectDocumentWorkflow(DocumentWorkflowItem wfItem)
           ExternalExternalDocumentEventArgument arg = new ExternalExternalDocumentEventArgument(wfItem.WorkflowID, wfItem.Document, User.CurrentUser);
           if (this.RaiseRejectDocumentWorkflowEvent != null)
               RaiseRejectDocumentWorkflowEvent.Invoke(this, arg);

       #region IDocumentService Members

       public event EventHandler<documenteventarguments /> Created;

       public event EventHandler<documenteventarguments /> Updated;

       public event EventHandler<documenteventarguments /> Archived;

       public event EventHandler<documenteventarguments /> Deleted;

       public event EventHandler<documenteventarguments /> SentToWorkflow;

       public event EventHandler<documenteventarguments /> Approved;

       public event EventHandler<documenteventarguments /> Rejected;


Administration Application Program

Image 6
private void approveToolStripMenuItem_Click(object sender, EventArgs e)
    _documentID = int.Parse(dgrdDocuments.CurrentRow.Cells["ID"].Value.ToString());
    DialogResult result = MessageBox.Show("Are you sure that you want to Approve this document", "confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
    Document doc = null;
    if (result == DialogResult.Yes)
        doc = _service.GetDocumentByID(this._documentID);
        if (doc.Status != DocumentBaseLibrary.Status.InWorkflow)
            MessageBox.Show(string.Format("This document {0} is not in Workflow state", doc), "Error", MessageBoxButtons.OK
                , MessageBoxIcon.Error);

  private void Approve(Document doc)
   DocumentWorkflowItem wfItem=_service.GetWorkflowItem(doc);

   // Get the type of the workflow
   Type type = typeof(DocumentWorkflow.DocumentWorkflow);

   // Start the workflow instance
   Guid id = wfItem.WorkflowID;
       WorkflowInstance inst = theWorkflowRuntime.GetWorkflow(id);

   catch(Exception ex)
       MessageBox.Show("The Document cannot be loaded. " + ex.ToString(),
           "Administration", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);


Workflow foundation, it take off completely the burden of all complexity of the

Application rules and decisions

But, to achieve the maximum reusability, you have to

prepare your code by creating services , and separate abstracted services in separate



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Refky Wahib
Software Developer (Senior) NSW Curriculum & Learning Innovation Centre
Australia Australia
I am a senior developer self taught,
the main study is electronics and communication engineering

I am working as senior programmer in center for learning and innovation

I develop Software since 1995 using Delphi with Microsoft SQL Server

before 2000, I didn’t like Microsoft tools and did many projects using all database tools and other programming tools specially Borland C++ Builder, Delphi, Power Builder
And I loved Oracle database for its stability until I was certified as Master in Database Administration from Oracle University.

I tried to work in web programming but I felt that Java is hard and slow in programming, specially I love productivity.

I began worked with .Net since 2001 , and at the same time Microsoft SQL Server 7 was very stable so I switched all my way to Microsoft Tech.
I really respect .Net Platform especially in web applications

I love database Applications too much
And built library with PowerBuilder it was very useful for me and other developers

I have a wide experience due to my work in different companies
But the best experience I like in wireless applications, and web applications.
The best Application I did in my life is Novartis Marketing System 1999 it takes 8 months developing with PowerBuilder and Borland C++, SQL Server
Performance was the key challenge in this Application.
The other 2 applications that I loved Multilingual Project in Scada company in Italy 2000 and SDP Mobile media content platform server for ChinaUnicom 2004
I hope that you enjoy any Article I post.
God bless you.

Comments and Discussions

QuestionWorkflow Database Required Pin
NehruKandasamy8-Nov-18 16:39
memberNehruKandasamy8-Nov-18 16:39 
QuestionWorkflow with id "xxxxxxxx" not found in state persistence store ???!!! Pin
arun_22083-Jan-18 3:21
memberarun_22083-Jan-18 3:21 
QuestionWorkFlow not present in the persistance store. Pin
asif rehman baber4-May-16 18:04
memberasif rehman baber4-May-16 18:04 
Questionhi Refky, good post but ,..... Pin
GREG_DORIANcod20-Oct-14 8:49
professionalGREG_DORIANcod20-Oct-14 8:49 
QuestionDocument approval workflow system without WWF or WCF Pin
KumarSundeep27-Feb-14 19:54
memberKumarSundeep27-Feb-14 19:54 
Questionrun Pin
Diya Ayesa3-Sep-13 22:30
memberDiya Ayesa3-Sep-13 22:30 
AnswerRe: run Pin
Altaf N Patel14-Oct-13 21:33
memberAltaf N Patel14-Oct-13 21:33 
Questionhelp Pin
Diya Ayesa3-Sep-13 22:24
memberDiya Ayesa3-Sep-13 22:24 
Questionpassword for the available users Pin
mohankumar jagan6-Nov-12 22:22
membermohankumar jagan6-Nov-12 22:22 
AnswerRe: password for the available users Pin
Member 11908785-Nov-13 23:50
memberMember 11908785-Nov-13 23:50 
GeneralMy vote of 5 Pin
Japinej29-Mar-12 21:40
memberJapinej29-Mar-12 21:40 
GeneralDocument approval workflow system Pin
Sen K Mathew23-Jul-09 21:25
memberSen K Mathew23-Jul-09 21:25 
AnswerRe: Document approval workflow system Pin
papapsi27-Aug-09 5:50
memberpapapsi27-Aug-09 5:50 
GeneralRe: Document approval workflow system Pin
Diya Ayesa3-Sep-13 22:28
memberDiya Ayesa3-Sep-13 22:28 
GeneralWorkflow db Pin
tp200619-Dec-08 12:33
membertp200619-Dec-08 12:33 
GeneralAproval list in admin prg is always empty Pin
ElGuroProgramador7-Oct-08 4:23
memberElGuroProgramador7-Oct-08 4:23 
GeneralRe: Aproval list in admin prg is always empty Pin
chuzon14-Jul-09 20:49
memberchuzon14-Jul-09 20:49 
Generalerror Pin
qasemt27-Aug-08 23:30
memberqasemt27-Aug-08 23:30 
GeneralRe: error Pin
Member 32323983-Sep-08 12:12
memberMember 32323983-Sep-08 12:12 
GeneralRe: error Pin
Pierreg0122-Nov-14 17:20
memberPierreg0122-Nov-14 17:20 
Generaltrouble in the database stuff! Pin
sameh_serag17-Mar-08 5:58
membersameh_serag17-Mar-08 5:58 
QuestionBug? Pin
Bart Meirens25-Feb-08 4:37
memberBart Meirens25-Feb-08 4:37 
GeneralMSSMSE to setup documentDB Pin
Numchucksasdf7-May-07 0:19
memberNumchucksasdf7-May-07 0:19 
JokeRe: MSSMSE to setup norkflowDB Pin
Numchucksasdf7-May-07 2:54
memberNumchucksasdf7-May-07 2:54 
Ok, renamed document_cate... to document_category and got past the 3 scripts.

WorkflowDB: Running the *.SQL C:\WINDOWS\Microsoft.NET\Framework\v3.0\Windows Workflow Foundation\SQL\EN files against a clean workflowDB...Confused | :confused:

ASP/XML/VB/Com Developer turned ASP/ADO/.Net'n c# Enthusiast

Dhahran,Saudi Arabia

AnswerRe: MSSMSE to setup documentDB Pin
Refky Wahib8-May-07 0:37
memberRefky Wahib8-May-07 0:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 9 Mar 2007


168 bookmarked