Quantcast
Channel: SharePoint Development & Ops » Visual Studio
Viewing all articles
Browse latest Browse all 5

SharePoint Designer Workflow Pause Until Date Change

$
0
0

So what happens when you’ve built out a SharePoint Designer workflow with a Pause Until Date activity, and then the date changes on you? Obviously, we want to the workflow to handle this situation and update the Delay logic accordingly.

Scenario

Let’s assume we have a Date Received column. After 2 weeks of initial item creation, we want to send a reminder notification to the user. So based on the value of Date Received column, add 14 days, and if Today matches that value, then send a notification. For this we need the Pause Until Date activity, so we do a quick evaluation, if there is a match, send the reminder, otherwise, pause until we reach that date.

  1. User enters Jan 13, 2012 in date received and creates new item.
  2. Workflow kicks off and determines that Jan 13, 2012 + 14 days is equal to Jan 27, 2012.
  3. Workflow checks if Today is equal to Jan 27, 2012. If it is, send the email reminder.
  4. If it isn’t, Pause Until Jan 27, 2012. You will see the pause activity log to the workflow history list the day it’s planning to pause until.

Perfect right, works great. Now what happens when a user edits the item and changes the date received to Jan 17, 2012. We want the delay to postpone until the 31st of Jan, not the 27th. I’ve read a great deal of posts that document how others have tackled this problem, ranging from an event handler to some sort of custom workflow logic. In our case, since the forms were custom ASP.NET forms submitting programmatically to the list, it was easy enough for me to track any changes to the date received column, and if I do find that it has changed, I simply stop and restart the workflow within the submit event. The logic looks like this:

// stop and restart the workflow if date received is different
if (dtcDateReceived.SelectedDate.ToShortDateString() != dateReceived.Value)
{
    // stop workflow
    SPWorkflowCollection itemWorkflowCollection= item.Workflows;
 
    SPWorkflowAssociation workflowAss = yearEnd.WorkflowAssociations[new Guid("369dcef7-b35c-48f1-bf2f-5b9f53d568ee")];
 
    foreach (SPWorkflow itemWorkflow in itemWorkflowCollection)
    {
        if (itemWorkflow.ParentAssociation.Id == workflowAss.Id && (itemWorkflow.InternalState & SPWorkflowState.Running) == SPWorkflowState.Running)
        {
            SPWorkflowManager.CancelWorkflow(itemWorkflow);
            site.WorkflowManager.StartWorkflow(item, workflowAss, workflowAss.AssociationData);
            break;
        }
    }
}

For completeness, this is how I set the hidden field on load that stores the original Date Received value.

// set date received to see if it changes
 dateReceived.Value = Convert.ToDateTime(yearEndItem["Date Received"]).ToShortDateString();

Thanks Tony for the original post that helped me build out the logic above:

http://www.tonytestasworld.com/post/Howto-Start-a-Sharepoint-Workflow-Programmatically.aspx


Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images