Friday, August 28, 2009

Get WorkFlow Task Item from List Item Id

 

        public static SPListItem WorkflowTaskItemBasedOnListItem(SPWeb web, string listName, string workflowTaskListName, int itemId)
        {
            SPListItem workflowItem = null;
            SPList workflowTasks = web.Lists[workflowTaskListName];
            SPList mainList = web.Lists[listName];
            SPListItem listItem = mainList.GetItemById(Convert.ToInt32(itemId));
            SPWorkflowFilter filter = new SPWorkflowFilter();
            filter.InclusiveFilterStates = SPWorkflowState.Running;
            // Get a list of workflow tasks associated with current Item
            SPWorkflowTaskCollection workTaskColl = web.Site.WorkflowManager.GetItemTasks(listItem, filter);
            foreach (SPWorkflowTask task in workTaskColl)
            {
                if (task != null)
                {
                    workflowItem = workflowTasks.GetItemById(task.ID);
                    break;
                }
            }

            return workflowItem;
        }

 

This function returns a workflow item based on Parent List item Id. It reads the List Item and finds the associated workflow which are still running against this list item.

9 comments:

Eric Willman said...

Any thoughts on how to go the other direction? i.e. I have the Task item, how do I get it's associated parent List Item?

Sanjay Sansanwal said...

Check this
http://sansanwal.blogspot.com/2009/08/function-to-read-list-item-id-based-on.html

Anonymous said...

wow much thanks for help, Sanjay!

Andrey Myasnikov
w e b [@] t e g o l a . r u

PrashanthSpark said...

Will i able to get Attachment file associated to Task , i need to copy this to new task again...

Sanjay Sansanwal said...

Once you have got an item, you could easily get its attachment, check the sample code-

public static DataTable GetAttachments(string listName, string itemNo)
{

DataTable tb = new DataTable();

using (SPWeb oWeb = WEB_OBJECT)
{
oWeb.AllowUnsafeUpdates = true;
tb.Columns.Add("Name");
tb.Columns.Add("Url");
tb.Columns.Add("SizeKB");

SPFolder folder = null;
try
{
folder = oWeb.Folders[listName];
}
catch
{
folder = oWeb.Folders["Lists"].SubFolders[listName];
}
try
{
folder = folder.SubFolders["Attachments"].SubFolders[itemNo];
foreach (SPFile file in folder.Files)
{
DataRow row = tb.Rows.Add();
row["Name"] = file.Name;
row["Url"] = oWeb.Url + "/" + file.Url;
row["SizeKB"] = ((file.Length / 1024) + 1).ToString() + " KB";
}
}
catch
{

}
oWeb.AllowUnsafeUpdates = false;
}

return tb;
}

Prasad Athalye said...

Any idea how to retrieve completed workflow related tasks?

Prasad Athalye said...

Any idea how to retrieve tasks for completed workflows?

Victor said...

I need to get all runnig tasks and iterate through them to set the outcome to "NotRequired". Any idea?

Anonymous said...

Thanks..... It worked....