Search code examples
c#visual-studio-2008sharepointsharepoint-2007sharepoint-workflow

How to get SPuser object from Assigned To field of task


Hi I have a sharepoint 2007 workflow and i need to get the SPuser object in order to send mails to the user but all i can get is a string from the task's assigned to field. How do i do this?

This is my code

foreach (SPWorkflow workflow in splistitem.Workflows)
{
     foreach (SPWorkflowTask task in workflow.Tasks)
     {
         string user = task["Assigned To"].ToString();
     }
}

Solution

  • string assignedToValue = task["Assigned To"].ToString();
    SPFieldUserValue userField = (SPFieldUserValue)workflow.Tasks.Fields["Assigned To"].GetFieldValue(assignedToValue);
    SPUser user= userField.User;
    

    To make it more robust, you can use SPBuiltInFieldId.AssignedTo instead of the hard-coded "Assigned To" value.