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:

  1. 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?

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

    ReplyDelete
  3. Anonymous7:45 pm

    wow much thanks for help, Sanjay!

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

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

    ReplyDelete
  5. 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;
    }

    ReplyDelete
  6. Any idea how to retrieve completed workflow related tasks?

    ReplyDelete
  7. Any idea how to retrieve tasks for completed workflows?

    ReplyDelete
  8. Victor11:02 pm

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

    ReplyDelete
  9. Anonymous7:43 am

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

    ReplyDelete