Approval Moderation in Sequential Workflow Project

Have you ever wondered how to automate Sharepoint approval process in a Sequential Workflow Project? Well look no futher this article is about that.

Lets start with a scenario to be clear, say we have a product list where someone in your company approves every product listing based on thresholds, that threshold measure can be a price range on a certain product type. Now that person usually declines desk product types when the price indicated by the contributor is below $200 and above $500, at this point the approver will do it manually by looking whether the product have violated the known threshold, this is an easy task for 10 items but imagine managing 300 of them. That is why we will be automating that based on definitions that we will set on a separate list. That list that will be creating will define threshold for different product type so if it hits any of the barriers I will decline the list item and the ones that does not will be auto approved. Sound simple? read further.

You might be already thinking that we can achieve that by using the Column Validation of a List. Well not quite as there are some limitations and one important one is the support for boolean operator. So a formula like this would work

=[Product Price] < 500

but this will not

=[Product Price] < 500 AND [Product Price] > 500

Now well you might think again that we can achieve that on the Sharpoint Designer. Well not quite as well as the Sharepoint Designer will only give you 1 search condition for a list lookup. So for example you need to lookup for a price threshold which meets the condition in multiple columns like for example a product type and maximum date of validity then it will be impossible.

So our last resort is using Visual Studio! So in this post I will explain to you how this will be achieved in the simplest manner. All you need is to follow this steps and I can assue you can make more complex scenarios after this one.

1. Create Lists for Product and Product Type

Product Type List

Product List

Take note that the column Product Type Column in Product List is a lookup on Product Type List

2. Enable Versioning in Product

As you can see we need to require content approval, we need to create a version each time (this ensures that you can roll back) and choose “only users who can approve items” this makes sure only the authorized can approve the items manually

3. Create a Task List

This is a library type in Sharepoint and the default one would do.

4. Create a Sequential Workflow Project

Choose Sequential Workflow under Sharepoint -> 2010

Follow the wizard

Choose the List you will invoke the workflow from and the associated Task List and History List

Trigger it on create and update

Now you will be presented with a Workflow Diagram

You can create more complex stuff by choosing items on the toolbar

but for this instance we will be making this straightforward and do everything on the onWorkflowActivated section. Now double click that and you will be presented with a code behind. Now copy the code below.

private void onWorkflowActivated1_Invoked(object sender, ExternalDataEventArgs e)
{

    SPLinqDataContext dc = new SPLinqDataContext(workflowProperties.SiteUrl);

    EntityList<ProductTypeItem> ProductType = dc.GetList<ProductTypeItem>("Product Type");

    string sProductType = GetLinkedListItemString(workflowProperties.Item["Product Type"].ToString());
    double dPrice = double.Parse(workflowProperties.Item["Product Price"].ToString());

    var Result = (from p in ProductType
                    where p.Title == sProductType && p.MaxDateValidity >= DateTime.Now
                    select p).SingleOrDefault();

    if (Result.MinPrice > dPrice)
    {
        workflowProperties.Item.ModerationInformation.Status = SPModerationStatusType.Denied;
        workflowProperties.Item.ModerationInformation.Comment = "Price Below Threshold - Saved on Approval Comment";
        workflowProperties.Item["Notes"] = "Price Below Threshold - Saved on Notes Field";
        workflowProperties.Item.Update();
    }
    else if (Result.MaxPrice < dPrice)
    {
        workflowProperties.Item.ModerationInformation.Status = SPModerationStatusType.Denied;
        workflowProperties.Item.ModerationInformation.Comment = "Price Above Threshold - Saved on Approval Comment";
        workflowProperties.Item["Notes"] = "Price Above Threshold - Saved on Notes Field";
        workflowProperties.Item.Update();
    }
    else
    {
        workflowProperties.Item.ModerationInformation.Status = SPModerationStatusType.Approved;
        workflowProperties.Item.Update();
    }

}

private string GetLinkedListItemString(string sItem)
{
    if (sItem.Contains("#"))
    {
        return sItem.Substring(sItem.LastIndexOf("#") + 1);
    }
    else
    {
        return sItem;
    }
}

If you notice I am uisng here LINQ to Sharepoint which you can refer to this post for a full tutorial, this makes my life easier in querying Sharepoint Lists

Now to explain some points on the code above:

  • To get and set item values on the current list item that is being processed use workflowProperties.Item["Column Name"]
  • To set Approval Status use workflowProperties.Item.ModerationInformation.Status
  • Since we are querying LINQ style, the search conditions can be as complex as you want and you are just limited by your imagination
  • You can also see that we use the Approval Comment field and the Notes Custom Field we created, I just wnat to demonstrate to you how to save comments on Approval fields as well as a field in your list.
  • If you use a column that is coming from a linked list it will show in this format ID;#Value (i.e. “1;#Test”)

Now other than that I guess the code above is straightforward as the only thing it does is when price is above or below threshold levels then it will decline the list entry and leave a note.

Run you project then put in your data.

5. Run your code

First Add items in Product Type to define thresholds

Add a new product that will violate your threshold

Now you can see the progress once its submitted

Then once its finished, you will see the note and the final approval status

Guide in developing Initiation Form on Sharepoint Workflows

Have you ever wondered whats the use of Initiation Forms on Sharepoint? Did you ever thought of creating a list item based on an item on another list? Do you want to extend the 2 buttons in Manual Workflow Initiation to have input forms?  Well that’s the use of the Initiation Forms and I will run you though a sample that will work for you.

So what is an Initiation form?  It simply a page that is presented when you start a workflow manually, the page where you see two buttons for “Start” and “Cancel”.  By default you have those buttons but you can extend this page by adding form fields and use the value on those as variables that you can consume for your workflow process.

So lets start with a sample, lets say you have the requirement to have an Office Supply list where your team can place an order on a specific item and that invokes a workflow on the backed, making a task list grabbing important details like Name of requester, quantity and the Item requested.  Here are the steps to achieve it.

1. Create an “Office Supplies” List

2. Now using Sharepoint Designer, go to your list and create a workflow.

3. Name your workflow, I called it “Supply Request Workflow”

4. Now Save and Publish your workflow and make sure it created a Task List and History List

5. Double check if you have the Task List by going to your Sharepoint instance

6. Now update that task list to cater for the following new fields. Item Requested, Quantity and Requested By.

7. Back to Sharepoint Designer and edit again your workflow

8. Now create an Initiation Form by clicking the Initiation Form Parameters

9. Now add the field, this is the fields that will populate your Task List as well on step 6, so create the same fields and its format you have on the said step.

10. Once done, time for you to set up an Action on Step 1, and we need to create a “Create List Item” action

11. Now choose the property “this list”.

12. Now choose the “Association: Task List”

13. Modify the Value sent to those fields

14. For example in the Title Field (“of the Task List”) you want to use the Current Item which is the “Office Supply List” Name.

15. You can add more strings of your choice if you want, so since this is a new request we make the title “New Item Requested : Your Item”.

16. Now for other fields like Quantity you can grab it from the initiation form you created in Step 8.  To do that just choose “Workflow Variables and Parameters” in Data Source and you will see the fields you created of on the “Field from source”.

17. Once all values are properly assigned click OK.

18. Now Save and Publish again.

19. Now usually manual initiation workflow starts by hitting the Workflows Button then choosing the appropriate workflow to run.  To make it a bit user-friendly I guess we will have to create a button on the Display Form Ribbon of an item.  Do that by clicking Custom Action and choose “Display Form Ribbon”.

20. Now name you button, I called it Request now.  And set it to initiate your “Supply Request Workflow”.

21. Add your icons.  Then your done.

22. Open now a list item on “Office Supplies” list, then you will see your “Request Now” button.  Click that.

23. Now you see your “Supply Request Workflow” with the input forms you had defined.

24. Click start to initiate the request, and you will see it on the “Task List”

25. With the fields populated as configured.

Sharepoint Workflow does not trigger on e-mail created List Items

I was testing a while ago the workflow and list incoming e-mail combo and found out that by default it seems that workflows does not automatically start on e-mail-enabled lists.  To verify whether this happens in your sharepoint instance you need to go to Sharepoint Powershell which you can find here (make sure run it as administrator)

and execute the following

stsadm -o getproperty -pn declarativeworkflowautostartonemailenabled

If you see the following result:

<Property Exist="Yes" Value="no" />

Then it means it indeed does not start on e-mail enabled list and you should enable that setting.  To do that just type this again on Powershell

stsadm -o setproperty -pn declarativeworkflowautostartonemailenabled -pv true

Then all should be fine

Step by Step Guide in Developing your own Workflow in Sharepoint 2010

Workflows are a real poweful tool built-in in Sharepoint since 2007 version as you can design it to add logic to your site or application without any custom coding!  This can be anything from automating business processes, sending notifications or even as simple as creating tasks.  There are a lot of possibilites which is only limited by your imagination.

This article will discuss how easy it is to develop your own workflow in Sharepoint 2010, you dont even have to be a developer as you will not write any custom codes and all you have to do is just a series of mouse point and clicks.  To get started we need an entry point in the workflow, this will act as a trigger to start the process and one good example that we can use is when someone add an item to a list.  So this sample solution we will be developing a workflow to trigger from a list and based on the selection on one of the list column values it will trigger an email to be sent and update the items status.  Using this example I guess we will be tackling most of the imporant aspects of workflows such as steps, conditions and actions.

So lets start!

Lets start by creating a list and here is the structure.

Now for a bit of explanation here is what each fields do

Title - That will be the subject of the email you will be sending
User Email - The recipient email address
Message To user – The message in the email
Status - Whats the status of this item (choice between Started, Email Sent, Processing, Resolved)
Send Email - A flag whether email will be sent to recipient.

Once you created the list fire up Sharepoint Designer and start creating your workflow.  First go to List and Libraries

Choose your list and go to the Workflow section then create new

It will ask you to define a Workflow name and description

Now, the workflow designer shows

.

Now if you notice on the ribbon there is a Condition and Action Button, there are the key elements for designing the workflow, examine the items inside and these as it will list all the possibilities you can do.

Workflow Action Items

Workflow Condition Items

On on your Step 1, we choose “Email users” (this means when the workflow starts it will email a certain user of your choice)

Now define the Email Message

On the “To” section, choose users.  At this point we choose the email address stored on the list item.

We select that “User Email” column you created on your list

Now on the CC, you will add the one who added the list item.

For the title, we will get it from the “Title” column in your list

And for the body you choose the “Message to user” column in yousr list as well as the “Modified by” field

Congratulations! Your done with you first action, now lets add a condition on that action

Click on the top of the first action you had created

Add a common condition “If any value equals value” which means if a certain column in that current item falls in a value that you declare an action will be triggered

Now the condition appears on top of your first action

Now modify the values to what you want, since you want an action to happen when “Send Email” field has a value of “Yes” then define as such.

If you had noticed the First action you created was not on the IF block, so you need to move it up, to do that choose the drop down on the side and choose move action up

This will then place it inside the IF block

Now in this scenario lets say you also want to update the field “Status” to “Email Sent” when the email sent.  To do that, add another action by choosing the “Set Feild in Current Item”.  Make sure that you also click below your first action so that when you add a new action it will be placed there.

Now set the field and value properties

Now for your ELSE block, choose the “Else-If Branch” from the ribbon

Now do the same action above but this time set the “Status” to “Processing”

Now your all done!  Save and publish your workflow

Then, wait while its validating

But wait you need a trigger on when the workflow will start.  Do that by going to the Workflow objects, choose your workflow and configure it on the start options

Now all you have to do is test, create a list item to trigger conditions you built

Then wait for the results

Thats it! its that easy, no coding required!

Follow

Get every new post delivered to your Inbox.

Join 774 other followers