Intro

Nintex Workflows is an awesome highly customizable product, that makes life of many people easier. But sometimes you need to write a few lines of code to integrate your forms with nintext workflows. There are few articles regarding this topic, but most of them use web service or some of the nintex dlls.

Code

There is an easier way to do this. I've took a code from here and modified it to suit my needs:

public static class SPWorkflowUtil
{
    private static int Attempts = 5;
    private static int MsTimeout = 100;
    private static bool IsSync = true;

    public static SPWorkflowTask GetCurrentUserNotStartedTask(SPListItem listItem)
    {
        var taskStatus = SPResource.GetString(new CultureInfo((int)listItem.Web.Language, false), Strings.WorkflowTaskStatusNotStarted, new object[0]).ToLowerInvariant();
        var wfTasks = listItem.Tasks;

        var task = wfTasks
            .Cast<SPWorkflowTask>()
            .Where(_ => _[SPBuiltInFieldId.AssignedTo] != null) // Task assigned to sombody
            .Where(_ => _[SPBuiltInFieldId.TaskStatus] != null) // Task has status
            .Where(_ => ((string)_[SPBuiltInFieldId.TaskStatus]).ToLowerInvariant() == taskStatus) // Task status is 'Not Started'
            .FirstOrDefault(_ => SPFieldUtil.GetLoginName(_, SPBuiltInFieldId.AssignedTo).ToLowerInvariant() == listItem.Web.CurrentUser.LoginName.ToLowerInvariant());

        return task;
    }

    public static bool AlterTask(SPListItem task, int outcome)
    {
        //Simple task outcome
        // 0 - Approved
        // 1 - Rejected

        // Flexi task outcome
        // 0 - None
        // 1 - Approved
        // 2 - Rejected

        Hashtable htData = new Hashtable();
        htData[SPBuiltInFieldId.Completed] = true;
        htData[SPBuiltInFieldId.PercentComplete] = 1;
        htData[SPBuiltInFieldId.TaskStatus] = SPResource.GetString(new CultureInfo((int)task.Web.Language, false), "WorkflowTaskStatusComplete", new object[0]);
        htData[new Guid("A7AE99D0-E5DF-47f4-9D75-560E3F608006")] = outcome;
        htData[new Guid("819E6CF2-36C3-4013-8AEF-C99712C26036")] = "(Completed)";

        if ((int)task[SPBuiltInFieldId.WorkflowVersion] != 1)
        {
            SPList parentList = task.ParentList.ParentWeb.Lists[new Guid(task[SPBuiltInFieldId.WorkflowListId].ToString())];
            SPListItem parentItem = parentList.Items.GetItemById((int)task[SPBuiltInFieldId.WorkflowItemId]);
            for (int i = 0; i < Attempts; i++)
            {
                SPWorkflow workflow = parentItem.Workflows[new Guid(task[SPBuiltInFieldId.WorkflowInstanceID].ToString())];
                if (!workflow.IsLocked)
                {
                    task[SPBuiltInFieldId.WorkflowVersion] = 1;
                    task.SystemUpdate();
                    break;
                }

                if (i != Attempts - 1)
                {
                    Thread.Sleep(MsTimeout);
                }
            }
        }

        var result = SPWorkflowTask.AlterTask(task, htData, IsSync);
        return result;
    }
}

With this code you can approve the task like this:

var list = GetList();
var listItem = list.GetItemById(id);
var task = SPWorkflowUtil.GetCurrentUserNotStartedTask(listItem);
if (task == null)
{
    return;
}

SPWorkflowUtil.AlterTask(task, 1);

To reject flexi-task, just use:

SPWorkflowUtil.AlterTask(task, 1);

It is possible to use this method for all types of nintex tasks. Just keep in mind that you need to pass different outcome to AlterTask method depending on a nintext task type:

Outcome Request Approval Flexi Task
Approve 0 1
Reject 1 2

Summary

Sometimes it easier to use a piece of code without external dependencies on third-party DLLs, especially when you just need a few guids from the dll.


;