Get all the WorkflowTask associated with a ListItem

Sometimes, we need to access all the Task associated with an SPListItem for a particular workflow. Following quick code is helpful.

//using the namespace

using Microsoft.SharePoint.Workflow;


SPSecurity.RunWithElevatedPrivileges(delegate
                    {
                        using(SPSite site = new SPSite(SPContext.Current.Web.Url))
                        {
                            using (SPWeb web = site.OpenWeb())
                            {
                                SPList lsPM = web.Lists["MyList"];
                                SPListItem lstMyList =   lsPM.GetItemById(IDOfTheListItem);
                                
                                SPWorkflowFilter filter = new SPWorkflowFilter();
                               //Filter task based on the Workflow Status
                               filter.InclusiveFilterStates = SPWorkflowState.Running; 
                               SPWorkflowTaskCollection workTaskColl = web.Site.WorkflowManager.GetItemTasks(lstMyList , filter);
                             
                              foreach (SPWorkflowTask task in workTaskColl)
                              {
                            if (task != null)
                            {
                                        // Loop through the Task Items
                            }
                        }
                            };
                        }
                    });


Hope this helps...........

Comments

Roopa said…
Hi,

I am not getting any active tasks with this code. I am trying to get list of tasks associated with the list item in SharePoint 2013. could you please suggest.
Hi Roopa,
This code shall get all the Tasks associated with an SPListItem.
//Filter task based on the Workflow Status
filter.InclusiveFilterStates = SPWorkflowState.Running; // This shall help you to filter tasks based on the status of the task.
This should work in all case if the List has any associated workflow tasks.

Regard....

Popular posts from this blog

SharePoint log error "Cannot find site lookup info for request Uri http://"

Visual Studio Build Issue : Build: 0 succeeded or up-to-date, 0 failed, 1 skipped

Reset your SharePoint Farm PassPhrase