3

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.

1
  • try SPBuiltInFieldId.WorkflowLink instead of SPBuiltInFieldId.WorkflowListId Jun 10, 2016 at 10:23

2 Answers 2

2

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.

4
  • Mihail, thank you are absolutely right. I recieved the result. And how can i get ItemId value in this string ?
    – Denis
    Jun 14, 2016 at 21:32
  • Depending on what programming language you are using, you should be able to convert the JSON string into an object that contains the following properties: ItemId, WebId, ListId. In JavaScript this very easy: JSON.parse(your_RelatedItems_field_value_here)[0].ItemId. In C# you need to deserialise the JSON string in order to convert it into a C# object. You can find plenty of articles on how to do this. Most of them will probably recommend using a framework/library like Json.NET (newtonsoft.com/json) which is perfectly fine to use. Jun 15, 2016 at 10:26
  • @Mihail-FlorinPopa Is it possible to get ItemId from string with "Related items" by Workflow 2013? For example like: "Get 0/ItemId from..."
    – Daniel N.
    Dec 3, 2019 at 10:07
  • @Daniel N. You should be able to. Try "Get 0/RelatedItems/ItemId"... Or try to extract the value of the RelatedItems field into a Dictionary workflow variable and then further extract the ItemId property. I think I have done this before, but I don't recall exactly how. You will have to try it out yourself. Dec 3, 2019 at 13:01
0

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);
            }

        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.