What is a workflow?
In simple words, a workflow is an electronic routing of documents from one concerned person to another. A document does not become visible to the general public unless it's approved by an authority. In large organizations, work is not managed by individuals but by teams. So it is important to release the document through a proper channel. Let me give you an example. In HR department of a company, suppose HR executive interviews a person for a possible position in the company. HR executive fills a form with candidate's information. That document is sent to HR Manager who scrutinizes the candidate by reading the information in thedocument. If he approves the document, document goes to a senior manager, who again reads through the document and approves it. After getting an approval from a senior manger, document containing candidate's information reaches the director who give the final appoval and the candidate is hired. Well, this is just one example.
SharePoint is used for colloboration and if it can support a workflow mechanism, then it's a wonderful tool to manage different business processes in your organization. SharePoint, currently, has a one level approval system, by default but custom code can be written to create a robust workflow mechanism. There are different types of workflows. One is a serial workflow. Serial workflow is the one in which a document goes from one person to another for approval. The other is parallel workflow. Parallel workflow is a mechanism where a document can be approved by any member of the team. Similary, there can be a requirement such as that document should be approved by all members of the team. This is what we call a parallet workflow mechanism.
There are many vendors who have developed excellent and robust workflow systems. Some of them are:
1. Skelta (www.skelta.com)
2. Nintex (www.nintex.com)
3. Captaris (www.captaris.com)
4. K2.NET (www.k2workflow.com)
Workflow application can be developed using two ways:
1. Use multiple lists. For example, use one list for new documents, one list for pending documents, and one for approved documents. Whenever a document arrives in the document library, capture it and move it to the other document library.
2. Second method is to use "Microsoft Event Handler Toolkit" (Toolkit contains samples that show you how to use event sinks). Capture document when it's added or updated and change it's approval status and at the same time send an email to the next approver notifying him about the document. You can store approvers in SQL Server or you can use XML configuration files.
If you want to know how SharePoint object model works, how you can use Event Handler Toolkit, how you can use SQL Server to store captured information, then read the following tutorial:
http://www.walisystems.com/articles/sps/spsdoclib.asp
Ok, let's get started. Below you will find the sample code for the workflow application.
Here is some sample code: (You can get complete event handling code from the tutorial link shown above)
private SPListItem spListItem = null;
protected override void HandleEvent()
{
switch (EventInfo.Type)
{
case SPListEventType.CheckIn:
OnCheckin();
break;
case SPListEventType.CheckOut:
OnCheckout();
break;
case SPListEventType.Copy:
OnCopy();
break;
case SPListEventType.Delete:
OnDelete();
break;
case SPListEventType.Insert:
OnInsert();
break;
case SPListEventType.Update:
OnUpdate();
break;
}
}
private void OnInsert()
{
try
{
SPFile spFile;
spFile = EventWeb.GetFile( EventFileUrl );
spListItem = spFile.Item;
//Get Approver from database
string strApprover = DB.GetApprover();
//set approval status to pending
spListItem["Approval Status"] = 2;
spListItem["Approver"] = strApprover; //this is a custom property added by you.
//call update to save your changes
spListItem.Update();
//Send an email
SendEmail();
}
}
Similarly you can write code in OnUpdate():
private void OnUpdate()
{
your code goes here ...
}
Code for sending an email:
public void SendMail()
{
MailMessage msg;
try
{
SmtpMail.SmtpServer = "smtp.yourserver.com";
msg = new MailMessage();
msg.BodyFormat = MailFormat.Html;
msg.From = currentWeb.Author.Email; //currentweb points to your current site
msg.Subject = strSubject;
msg.Body = "A document needs your approval.";
msg.To = strApprover; //strApprover contains the pending approver
SmtpMail.Send(msg);
}
catch (Exception ex)
{
//error handling ...
}
}
Please note that this not a tutorial that will give you each and every step for creating a workflow solution. This article just gives you some tips that tell you how to create a workflow. Read the following tutorial if you want to learn more about workflow solutions:
http://www.walisystems.com/articles/sps/spsdoclib.asp