petauro
petauro

Reputation: 31

SPListItem.Tasks always empty

I have a custom sharepoint (2007) list (named testlist) on which I attached a test workflow (built with sharepoint designer 2007 and named testwf), which only task defined in the 'Actions' section at 'Step 1' is to wait until april 2014. When I add a new item to the testlist the testwf will start and, when I switch to the grid view, the item has the field "testwf" as running.

Now I need to access the workflow associated with the item and then "complete" this task via code by changing its status but, using the following code, I always get the item.Tasks list empty (but I can see that the internal variable m_allTaskListTasks has 1 element).

        using (SPSite site = new SPSite("http://mysp"))
        {
            site.AllowUnsafeUpdates = true;
            SPWeb web = site.OpenWeb();
            web.AllowUnsafeUpdates = true;

            foreach (SPList list in web.Lists)
            {
                if (list.Title != "testlist") continue;
                foreach (SPListItem item in list.Items)
                {
                    item.Web.AllowUnsafeUpdates = true;
                    if(item.Tasks.Count > 0) 
                       //do work
                }
            }
        }

Maybe I'm missing something...

Upvotes: 1

Views: 2942

Answers (3)

nsturdivant
nsturdivant

Reputation: 273

Cross-posted question.

@petauro, have you made any headway on this? I can corroborate @moontear's answer based on the following code that I have used with success in the past:

...
// get workflow tasks for SPListItem object item
if (item != null && item.Workflows != null && item.Workflows.Count > 0)
{
    try
    {
        var workflows = site.WorkflowManager.GetItemActiveWorkflows(item);
        foreach (SPWorkflow workflow in workflows)
        {
            // match on some indentifiable attribute of your custom workflow
            // the history list title is used below as an example

            if (workflow.ParentAssociation.HistoryListTitle.Equals(Constants.WORKFLOW_HISTORY_LIST_TITLE))
            {
                var workflowTasks = workflow.Tasks;
                if (workflowTasks != null && workflowTasks.Count > 0)
                {
                    // do work on the tasks
                }
            }
        }
    }
    catch
    {
        // handle error
    }
}
...

While only slightly different from the code you posted in your latest comment, see if it helps.

Another minor point: are there multiple instances of lists titled "testlist" within your SPWeb? If not, why iterate over web.Lists? Just get the one list directly and avoid some superfluous CPU cycles: SPWeb.GetList()

Upvotes: 1

Mulder
Mulder

Reputation: 307

I use this code to access my workflowtasks:

Guid taskWorkflowInstanceID = new Guid(item["WorkflowInstanceID"].ToString());
SPWorkflow workflow = item.Workflows[taskWorkflowInstanceID];
// now you can access the workflows tasks
SPTask task = workflow.Tasks[item.UniqueId];

Upvotes: 1

Dennis G
Dennis G

Reputation: 21788

You have to go differently about this. You need to get the workflow task list and retrieve your task from there and finish it.

First you would need to check whether a workflow is running on your item: if (item.Workflows > 0) from there you could iterate through all the workflow instances on the list item, get the SPWorkflowAssociation and the associated task and history list. From there you would only need to find the task you are looking for in the associated task list.

Upvotes: 0

Related Questions