How to get SPuser object from Assigned To field of task

Guruparan Giritharan picture Guruparan Giritharan · Jul 24, 2012 · Viewed 10.1k times · Source

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

Answer

Marek Grzenkowicz picture Marek Grzenkowicz · Jul 24, 2012
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.