salary advanceworkflow

19
Salary Advance Workflow Ifeanyi I Nwodo (B.Eng, MCSN,OCA,OCP, MCPD,MCTS,OCJP) 07033798594, 08187693785 . [email protected] http://www.facecompete.com http://alvana.facecompete.com http://sharepointbi.facecompete.com This Article is a continuation of an online video that demonstrated how to create Custom list you can view the video here or download it here In this Article I will show you how to create a SharePoint Workflow based on a List. I will be using the Advance Salary list I created in the video and will also be utilising the web application and its Site collection URL I created in the tutorial. So let’s get started. Scenario: —Create a Salary Advance Work Flow by which employees can request Salary Advancement, request mailed to accounts personnel, reply of either approved or rejected corresponded to employee— Create a New SharePoint Project in Visual studio. You can Name SalaryAdvance Define the url to the Web Application in SharePoint server (Contains the List we will be using). Slect Define as Farm Solution (Workflows are farm based solution).

Upload: ifeanyi-i-nwodo

Post on 06-Jul-2015

121 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Salary advanceworkflow

Salary Advance Workflow

Ifeanyi I Nwodo (B.Eng, MCSN,OCA,OCP, MCPD,MCTS,OCJP) 07033798594, 08187693785 .

[email protected]

http://www.facecompete.com http://alvana.facecompete.com http://sharepointbi.facecompete.com

This Article is a continuation of an online video that demonstrated how to create Custom list you can

view the video here or download it here

In this Article I will show you how to create a SharePoint Workflow based on a List. I will be using

the Advance Salary list I created in the video and will also be utilising the web application and its Site

collection URL I created in the tutorial. So let’s get started.

Scenario:

—Create a Salary Advance Work Flow by which employees can request Salary Advancement, request

mailed to accounts personnel, reply of either approved or rejected corresponded to employee—

Create a New SharePoint Project in Visual studio. You can Name SalaryAdvance

Define the url to the Web Application in SharePoint server (Contains the List we will be

using).

Slect Define as Farm Solution (Workflows are farm based solution).

Page 2: Salary advanceworkflow

Click finish.

Now our new SharePoint projected is created. Next thing to do is to add aworkflow item to it. For

this article we will be using A sequential Workflow, since our scenario and associated logic is simple.

Right click on project,

Point to Add and click on New Item.

See below.

Click on Sequential Workflow

Page 3: Salary advanceworkflow

Name it. Example SalaryAdvance

Click Add

Specify List Workflow for the workflow template

Click Next

Select the List to associate with the Workflow

Page 4: Salary advanceworkflow
Page 5: Salary advanceworkflow

Click Finish

Now your window should be similar to the following

Note : You can rename the feature 1 to AdvanceSalary as shown in the illustration above

Our Sequential workflow kicks off with on workflowActivated1 item, usually the default. However

we will need to do more to achieve a truly Salary Advance workflow.

First we are going to define a while loop that will hold our workflow recurring activities, when the

workflow is activated

Page 6: Salary advanceworkflow

Rename it to WhileNotCompleted because it is meant to loop until the workflow tasks are

completed.

The while only allows one activity, however I may want to execute more than one activity. So I will

use a sequence activity which is an activity item encapsulating single or multiple activity.

Page 7: Salary advanceworkflow

Now I can enter activities that will allow me to check for a change in state within the workflow and

perform a task I will add onworkflowitemchanged item and ifelse activity see below:

As you can see there are warning signs on the added activities, reason being that I am yet to set

condition and token.

Click on the while activity

On the properties window click on the Condition option and choose Code Condition.

Page 8: Salary advanceworkflow

Expand the Code Condition option from the properties window. Click on Condition option

and type a method name for the condition example OnWhile and press enter.

Type in the displayed method to enable the while

Page 9: Salary advanceworkflow

private void OnWhile(object sender, ConditionalEventArgs e) { e.Result = true;

}

Now to the onWorkflowItemChanged. For this item you will need to tag it, in other words

you need to specify its correlation token. Do this

Click on onWorkflowItemChanged

Click on the correlationToken option on the properties window and type

workflowToken

Now let’s work on the if here I am going to rename the if branches to correspond with the advanced

salary status which are:

Approved

Rejected

Initiated

In all cases the activity will be logged and the appropriate persons informed the status of workflow.

Now rename the if branches as

ifApproved

ifRejected

Also create conditions for them with similar names not the same ones.

Page 10: Salary advanceworkflow

The first ifBranch

The second ifelseBranch

Now let’s work on the individual ifbranches by parsing the activities that will be executed when the

various if condition s are met, this activities include :

Login the activity,

Sending a mail to the initiator and

Completing/terminating the workflow

Before we proceed we will need to create a field that will hold a status value when any change occur

in the workflow. Do the following:

Click on the onWorkflowItemChanged

Click on the bind properties of the AfterPropertis option on the properties window

Page 11: Salary advanceworkflow

On the resulting dialog box click bind to a new member(see below)

Click create Field

Click ok

Repeat the same steps to create a field for the BeforeProperties.

With that achieved, it’s time to populate the if conditions methods and determine their result

based on the value of the Status. Open the IfStatusApproved method you created earlier for the

ifApproved ifelse branch and enter the following:

private void IfStatusApproved(object sender, ConditionalEventArgs e) { string status = onWorkflowItemChanged1_AfterProperties1["Status"].ToString(); if (status == "Approved") { e.Result = true; } else { e.Result = false; } }

Repeat a Similar thing for the ifRejected Condition and apply the following: private void IfStatusRejected(object sender, ConditionalEventArgs e) { string status = onWorkflowItemChanged1_AfterProperties1["Status"].ToString(); if (status == "Rejected") { e.Result = true; } else { e.Result = false; }

}

Page 12: Salary advanceworkflow

Both methods sought to test the values in the status and based on that execute the conditions.

Let’s apply the activities that will execute tasks based on the conditions:

Login the activity,

Sending a mail and

Completing/terminating the workflow

Add the following activities from the tool box to both branches :

logToHistoryListActivity

sendmail

TerminateActivity

Rename them appropriately

Page 13: Salary advanceworkflow

Let’s add an ifelse branch that will be responsible for sending mail to the accounts

department when the workflow is initiated by an employee.

o Add an ifelse branch, rename it to ifInitiated, add a log activity history and send

email to it. Also add its correlation token and condition, you can use the method

name ifStatusInitiated, and as done above specify the content below for the

method.

private void ifStatusInitiated(object sender, ConditionalEventArgs e) { string status = onWorkflowItemChanged1_AfterProperties1["Status"].ToString(); if (status == "Initiated") { e.Result = true; } else { e.Result = false; } }

Your Workflow Design should look like:

Page 14: Salary advanceworkflow

Now let’s work on the content of the ifelse branches. The Logs are logged automatically they need

no codes/method, the activity terminate also have predefined task hence they need no methods,

but the send mail do, so let’s complete that.

Define CorrelationToken for the send mail activities as was done earlier

foronworkflowitemchanged.

Generate Handlers for the sendApprovalEmail

o To achieve this:

Right Click on sendApprovalEmail

Click Generate Handlers

Page 15: Salary advanceworkflow

Type the following into the method:

private void sendApprovalEmail_MethodInvoking(object sender, EventArgs e) { //Create an Item object based on our List SPListItem item = onWorkflowActivated1.WorkflowProperties.Item; //get the Employee Field Colunm from our List SPFieldUser assignedto = (SPFieldUser)item.Fields["Employee"]; //get the field value content of Accountant Clerk SPFieldUserValue user = (SPFieldUserValue)assignedto.GetFieldValue(item["Employee"].ToString()); //get the Requested Date Field Colunm from our List SPFieldDateTime requestedDate = (SPFieldDateTime)item.Fields["Requested Date"]; //get the field value content of Requested Date string rdt = requestedDate.GetFieldValue(item["Requested Date"].ToString()).ToString(); //get the Amount Field Colunm from our List SPFieldCurrency amount = (SPFieldCurrency)item.Fields["Amount"]; //get the field value content of Amount string amt = amount.GetFieldValue(item.Fields["Amount"].ToString()).ToString(); //get employee email, assign mail subject, and body string assigneeEmail = user.User.Email; sendApprovalEmail.To = assigneeEmail; sendApprovalEmail.Subject = "Advance Salary Request Approved"; sendApprovalEmail.Body = "Salary Advance Request with ID " + onWorkflowActivated1.WorkflowProperties.Item.ID.ToString() + ".\n Requested Date :" + rdt + ".\n\n Amount Requested :"+amt+".\n\n Has been Approved.";

Page 16: Salary advanceworkflow

}

Generate Handlers for the sendRejectedEmail

o To achieve this:

Right Click on sendRejectedEmail

Click Generate Handlers

Type the following into the method:

private void sendRejectedEmail_MethodInvoking(object sender, EventArgs e) { //Create an Item object based on our List SPListItem item = onWorkflowActivated1.WorkflowProperties.Item; //get the Employee Field Colunm from our List SPFieldUser assignedto = (SPFieldUser)item.Fields["Employee"]; //get the field value content of Accountant Clerk SPFieldUserValue user = (SPFieldUserValue)assignedto.GetFieldValue(item["Employee"].ToString()); //get the Requested Date Field Colunm from our List SPFieldDateTime requestedDate = (SPFieldDateTime)item.Fields["Requested Date"]; //get the field value content of Requested Date string rdt = requestedDate.GetFieldValue(item["Requested Date"].ToString()).ToString(); //get the Amount Field Colunm from our List SPFieldCurrency amount = (SPFieldCurrency)item.Fields["Amount"]; //get the field value content of Amount string amt = amount.GetFieldValue(item.Fields["Amount"].ToString()).ToString(); //get employee email, assign mail subject, and body string assigneeEmail = user.User.Email; sendApprovalEmail.To = assigneeEmail; sendApprovalEmail.Subject = "Advance Salary Request Rejected"; sendApprovalEmail.Body = "Salary Advance Request with ID " + onWorkflowActivated1.WorkflowProperties.Item.ID.ToString() + ".\n Requested Date :" + rdt + ".\n\n Amount Requested :" + amt + ".\n\n Has been Rejected."; }

Also generate Handlers for the sendInitiatedEmail

o To achieve this:

Right Click on sendInitiatedEmail

Click Generate Handlers

Type the following into the method:

Page 17: Salary advanceworkflow

private void sendInitiatedEmail_MethodInvoking(object sender, EventArgs e) { //Create an Item object based on our List SPListItem item = onWorkflowActivated1.WorkflowProperties.Item; //get the user Field Colunm from our List SPFieldUser assignedto = (SPFieldUser)item.Fields["Accountant Clerk"]; //get the field content of Accountant Clerk SPFieldUserValue user = (SPFieldUserValue)assignedto.GetFieldValue(item["Accountant Clerk"].ToString()); string assigneeEmail = user.User.Email; sendApprovalEmail.To = assigneeEmail; sendApprovalEmail.Subject = " Salary Advance Request "; sendApprovalEmail.Body = "Advance Salary Request <br/><br/>Request ID:" + onWorkflowActivated1.WorkflowProperties.Item.ID; }

That’s all. We can now build and deploy.

Simply right click on the project and choose build, repeat the same steps but choose deploy to

deploy it to the SharePoint server .see below.

From the Site collection features of our SharePoint server you can see our new feature running

Page 18: Salary advanceworkflow

We can have our workflow started by creating item on the Advance Salary List.

Page 19: Salary advanceworkflow

Until we meet a gain from me to you its Nkoma……..

Download complete sandboxed project here.