locked
Custom Workflow Approval Task GUID value changing? RRS feed

  • Question

  • Hello,

       I have a custom workflow that I'm developing with VS2010, and I'm having an issue that I can't explain or resolve.

    I've created a sequential custom workflow that creates a task in SharePoint, using the CreateTask activity (from the Toolbox in the workflow designer). I am binding the TaskId to a GUID field (through the Properties pane), and I set this GUID when the workflow is activated (by using the GUID.NewGUID() method in code). I also bind a MethodInvoking event handler to fire some code when the task is being created. This code is as follows:

     

     

            private void createSupervisorTask_MethodInvoking(object sender, EventArgs e)
            {
                createSupervisorTask.TaskProperties.TaskType = 1;
                createSupervisorTask.TaskProperties.AssignedTo = this._strSupervisor;
                createSupervisorTask.TaskProperties.Title = "Review for " + this._strSupervisor;
                createSupervisorTask.TaskProperties.Description = "test description";
                createSupervisorTask.TaskProperties.HasCustomEmailBody = true;
                createSupervisorTask.TaskProperties.EmailBody = EmailBody(FRM_SUPERVISOR_APPROVAL, workflowProperties.TaskListId,
                                                                            createSupervisorTask.TaskId,
                                                                            workflowProperties.Item.Name);
                createSupervisorTask.TaskProperties.SendEmailNotification = true;
            }
    
    
            private string EmailBody(string TaskPage, Guid listId, Guid TaskId, string DocName)
            {
                // Get publishing comments
                string strComments = GetPublishingComments(workflowProperties.Item);
    
                string url = string.Format("{0}/_layouts/BPMSWorkflow/{3}.aspx?List={1}&TaskID={2}",
                                           workflowProperties.Web.Url, listId.ToString(), TaskId.ToString(), TaskPage);
    
                string strDocURL = string.Format("{0}/{1}",
                                           workflowProperties.Web.Url, workflowProperties.ItemUrl);
    
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.Append(@"<html xmlns:v=""urn:schemas-microsoft-com:vml"" xmlns:o=""urn:schemas-microsoft-com:office:office"" xmlns:w=""urn:schemas-microsoft-com:office:word"" xmlns:m=""http://schemas.microsoft.com/office/2004/12/omml"" xmlns=""http://www.w3.org/TR/REC-html40"">");
                sb.Append(@"<head>");
                sb.Append(@"<meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8"">");
                sb.Append(@"</head>");
                sb.Append(@"<body lang=""EN-US"" link=""#003399"" vlink=""#AA00AA"">");
                sb.AppendFormat(@"<h4>{0} has requested Approval of pending changes to the {1} document.</h4><br /> ", this._strRequestor, DocName);
                sb.AppendFormat(@"<h4>Review the <a href=""{0}"" >{1}</a> document.</h4>", strDocURL, strDocURL);
                sb.AppendFormat(@"<h4>Reason for Change: {0}</h4><br /> ", strComments);
    
                sb.AppendFormat(@"<a href=""{0}"" >Click here to Approve</a>", url);
    
                sb.Append(@"</body>");
                sb.Append(@"</html>");
                return sb.ToString();
            }
    
    


    The intent is that the EmailBody() method generates the body of an email that is sent to a Supervisor, who can click on a link that displays a custom approval page (ASPX page), which I have in the Layouts area in my project. 

    The problem I am experiencing is that when the user clicks on the link in their email to approve the task (which displays my custom ASPX page), I run some code to reference the task (so I can set the Status on the task to Completed), and the GUID for the task (which is passed via a query string variable - TaskID) does not match the GUID that the task was actually created with. Here is the Page_Load() method for my custom ASPX page:

     

     

                //Get the task list using the list id from the query string
                try
                {
                    TaskList = Web.Lists[new Guid(Request.QueryString["List"])];
                }
                catch { }
    
                //and the actual task item
                if (Request.Params["TaskID"] != null)
                {
                    string strTempGUID = Request.Params["TaskID"].ToString();
                    Guid gTempGUID = new Guid(strTempGUID);
    
                    TaskItem = TaskList.GetItemByUniqueId(gTempGUID);
                }
    
    

     


    The code throws an exception when calling the GetItemByUniqueID() method, as the GUID I'm using (from the query string passed to the page) is not the same as the GUID for the task item I need to retrieve. If I iterate through the Task list items, I can see the item I want to retrieve, and the UniqueID GUID value for that item is different than the GUID I am passing via the query string.

    I don't understand why the task item would have a different GUID value than what I used when the task was created (via the workflow). This is causing me problems, as I can't seem to reference the task list item I want in my ASPX page. Does anyone have any suggestions on how to resolve this problem, or any workarounds that I can implement?

     

    Thanks in advance.

    John

     

    • Edited by JohnAdali Wednesday, October 12, 2011 3:26 PM
    Wednesday, October 12, 2011 3:21 PM

Answers

  • i've seen that error before when a lookup column is set incorrectly (to a value that no longer exists in the list).

    does anything change if you alter some of the properties you're setting for the task like the task type, assigned to, etc...?

    the only other thing off the top of my head i can think of is the email.  i usually use separate email events after the tasks are created, maybe it is in there?

    these vague errors can drive you batty, i know i've been there plenty of times myself.  :)


    Justin Joyce SharePoint Solution Architect http://lantekcs.com
    • Marked as answer by Zizhuoye Chen Thursday, October 20, 2011 2:17 AM
    Thursday, October 13, 2011 12:35 AM

All replies

  • rather than creating a new GUID with you association members when the workflow fires for the task here is what i do:

    public Guid TaskId = default(Guid);
    


    then during MethodInvoking for the create task event:

           private void createSupervisorTask_MethodInvoking(object sender, EventArgs e)
            {
               TaskId = Guid.NewGuid();
    ...

    perhaps this is an order of operations issue where the old value is lost at some point prior to the method invoking?

    also, just as a sanity check, you did go to your design surface, click on your create task activity, and assign the TaskID property in the Properties window to your TaskID member in the code behind, right?  sometimes i forget to hookup these associations.

    one last thing that might be worth checking if your correlation tokens, make sure they are set correctly and have the proper OwnerActivityName.

     


    Justin Joyce SharePoint Solution Architect http://lantekcs.com
    Wednesday, October 12, 2011 8:51 PM
  • Justin,

        Thanks for your reply. Yes, I did try the sanity checks you suggested. I verified that the TaskID property was wired to a variable via the Properties window in the designer surface, as well as made sure the correlation tokens were hooked up properly (including OwnerActivityName).

     

       I also tried your suggestion of setting the TaskId in the MethodInvoking function, but I still get the error. Oddly, the error message I get is "Value does not fall within the expected range", which isn't what I would expect (I'd expect a message like 'object not found' or similar).

     

     

    Wednesday, October 12, 2011 9:33 PM
  • i've seen that error before when a lookup column is set incorrectly (to a value that no longer exists in the list).

    does anything change if you alter some of the properties you're setting for the task like the task type, assigned to, etc...?

    the only other thing off the top of my head i can think of is the email.  i usually use separate email events after the tasks are created, maybe it is in there?

    these vague errors can drive you batty, i know i've been there plenty of times myself.  :)


    Justin Joyce SharePoint Solution Architect http://lantekcs.com
    • Marked as answer by Zizhuoye Chen Thursday, October 20, 2011 2:17 AM
    Thursday, October 13, 2011 12:35 AM