Question

I am starting workflow for a list item.

Workflow creats tasks.

How can i get a related item (list item) of a task programmaticaly?

SPBuiltInFieldId.WorkflowListId does no work.

Was it helpful?

Solution

Assuming your workflow is an out-of-the-box Approval workflow or a workflow that has been created using the 2010 platform (containing the "Start Approval Process" action), then you need to use the Workflow Item ID field (internal name is WorkflowItemId).


If your tasks are created by a workflow using the 2013 platform containing the "Assign a task" or the "Start a task process" actions, then you want to use the Related Items field (internal name is RelatedItems).

This field contains a JSON string that looks like this: [{"ItemId":2,"WebId":"c4762c49-735d-4672-ba09-9b58c08829ea","ListId":"f1b87a67-4415-4882-903c-fb2bc3caf002"}]. As you can see the JSON string is an array containg an object with a property called ItemId which is the id of the item that your workflow ran on to create the task.

OTHER TIPS

Thanks to Mihail.

After week of hard work my code solution is (for Sharepoint 2013).

namespace ER1.EventReceiver6
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class EventReceiver6 : SPItemEventReceiver
    {
        /// <summary>
        /// An item was updated.
        /// </summary>
        public override void ItemUpdated(SPItemEventProperties properties)
        {
            base.ItemUpdated(properties);       
            if (properties.ListTitle == "Tasks")
            {
            SPList Listcurrent = properties.Web.Lists["Tasks"];
            SPListItem taskid = Listcurrent.GetItemById(properties.ListItemId);
                bool flag = Convert.ToBoolean(taskid["updated"]);// we need "updated" to stop infinity loop
                    if (flag == false) // Already updated? No actions
                {          
                string jsonString = properties.ListItem[SPBuiltInFieldId.RelatedItems].ToString();

           int position2 = 0;   

           position2 = jsonString.IndexOf("WebId");    

            string IDSTRING= jsonString.Substring(11, position2 - 13);// bad way, but quick.


            SPList parent = properties.Web.Lists["Questions"];


                SPListItem parentItem = parent.GetItemById(Convert.ToInt32(IDSTRING));

                taskid["_x041d__x0430__x0437__x0432__x04"] = parentItem["Title"];
                taskid["_x0414__x043e__x043a__x043b__x04"] = parentItem["AssignedTo"];
                taskid["_x041f__x0440__x0438__x0433__x04"] = parentItem["_x041f__x0440__x0438__x0433__x04"];
                taskid["_x0421__x043e__x0433__x043b__x04"] = parentItem["_x0421__x043e__x0433__x043b__x04"];
                taskid["_x041f__x0440__x043e__x0435__x04"] = parentItem["Body"].ToString();

                taskid["updated"] = 1;// flag for check
                CopyItem(parentItem,taskid);
                try

                {
                    taskid.Update();               
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Exception Message: " + ex.Message);
                }
            }
            }
        }

        public void CopyItem(SPListItem srcItem, SPListItem destItem)
        {
            foreach (string attachmentName in srcItem.Attachments)
            {
                SPFile file = srcItem.ParentList.ParentWeb.GetFile(srcItem.Attachments.UrlPrefix + attachmentName);
                byte[] data = file.OpenBinary();
                destItem.Attachments.Add(attachmentName, data);
            }

        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top