locked
Get user's email from “assigned to” field at task workflow of sharepoint RRS feed

  • Question

  • I need to get user's email from "assigned to" field at task workflow of sharepoint For example when i send email, i use this code:

     

    SPListItem wfItem = onWorkflowActivated1.WorkflowProperties.Item;
                SPFieldUser assignedTo = (SPFieldUser)wfItem.Fields["Assigned To"];
    
                SPFieldUserValue user = (SPFieldUserValue)assignedTo.GetFieldValue(wfItem["Assigned To"].ToString());
                string assigneeEmail = user.User.Email;
    
                sendEmailToAssignee.To = assigneeEmail;
                sendEmailToAssignee.Subject = "New work order has been created.";
                sendEmailToAssignee.Body"Bla bla bla";
    in "Assigned To" field ( i created) in LitsItem, i have value domain\my_account so I can get my email my_mail@example.com to variable assigneeEmail. Now I need it but in task. In "Assign To" field in task, how can we go it?

    Thursday, December 22, 2011 4:29 AM

Answers

All replies

  • Hi,

    What is value you are having in assigneeEmail?

    Also is the Assigned To field part of item on which workflow runs and you want the same value in the Task's Assigned To column?

     


    Thanks, Santhosh_SGT
    Thursday, December 22, 2011 8:20 AM
  • I think he means that he has a domain account. In domain account in Email field he has an email. If we want to send an email, we need value "my_mail@example.com" set to variable assigneeEmail. But in field Assigned To (he created), there will be domain account for example his_company\his_account. So if that domain account is set to assigneeEmail, the email could not be sent. We need email address. That's why this code

    
    

    //get the Assigned To column and casts it into a SPFieldUser object
    
    SPListItem wfItem = onWorkflowActivated1.WorkflowProperties.Item;
                SPFieldUser assignedTo = (SPFieldUser)wfItem.Fields["Assigned To"];
    	//the value of the field is cast into a  SPFieldUserValue object
    
                SPFieldUserValue user = (SPFieldUserValue)assignedTo.GetFieldValue(wfItem["Assigned To"].ToString());
                string assigneeEmail = user.User.Email;

    
    

    do the job - get email field from account domain. But this code is for a field in ListItem. I also wonder how to do the same with task (get email address form Assigned To field in task form edit.

    Thursday, December 22, 2011 10:20 AM
  • If you have the domain account you can use a code similar to below to get SPUser object.

    SPUser assignedUser = this.workflowProperties.Web.EnsureUser("domain\account"); 

    Access Email property of assignedUser to get the email address required.


    Thanks, Santhosh_SGT
    Thursday, December 22, 2011 2:31 PM
  • In that code, you type account domain directly, i mean how can we get account domain form "assigned To" field in task edit form. Then access Email property of domain account.

    Can fix code for me.

    Friday, December 23, 2011 4:18 AM
  • Hi,

    Try using code similar to below to get the domain\acck

    SPListItem wfItem = this.WorkflowProperties.Item;
    string assigneeAccount = wfItem["Assigned To"].ToString();
    SPUser assignedUser = this.workflowProperties.Web.EnsureUser(assigneeAccount);

     


    Thanks, Santhosh_SGT
    Friday, December 23, 2011 7:36 AM
  • Follow your code:

    assigneeAccount  will get field "assigned To" of ListItem (This list item I created and it has "assigned To" field that i created by myself). I need assigneeAccount get "assigned To" field of Task edit form. (this field is default when we open task edit form).

    I found this code

    SPFieldUser assignedTo = (SPFieldUser)task.Fields[SPBuiltInFieldId.AssignedTo];

     SPFieldUserValue user = (SPFieldUserValue) assignedTo.GetFieldValue(task[SPBuiltInFieldId.AssignedTo].ToString());

    SPUser userObject = user.User;

     

    I thought it could do this job. But i don't know what "task" is. Is that a name of CreateTask component?

    I'm new so i'm highly appreciate you're helping me. Thank you.

    Friday, December 23, 2011 8:11 AM
  • If you want the task's Assigned To field you have to take instance of task listitem.

    Instead of,

    SPListItem wfItem = this.WorkflowProperties.Item;

    use below line,

    SPListItem taskItem = this.workflowProperties.TaskList.GetItemById(id);

    For getting the id value use the ListItemId property of Create Task activity.

    Let me know if you need anymore clarification.


    Thanks, Santhosh_SGT
    • Proposed as answer by thanhduc Wednesday, December 28, 2011 3:27 AM
    Friday, December 23, 2011 12:31 PM
  • Thank you very much. Now i'm off for a vacation, I'm back in Monday and check your code.

    Stay in this topic, please. May be i still need you help.

    Saturday, December 24, 2011 5:26 PM
  • when I type id, there is error, the name 'id' does not exist in the current context. Is the 'id' variable?

    Can you write full demo (i think it's just several code lines) to get email from Assigned To field of instance of task listitem. I have to finish it right now.

    I'm new and now I have to learn as much as possible at same time. I don't really have good basic knowledge about it. Now i'm learning.

    Monday, December 26, 2011 2:36 AM
  • when I type id, there is error, the name 'id' does not exist in the current context. Is the 'id' variable?

    Can you write full demo (i think it's just several code lines) to get email from Assigned To field of instance of task listitem. I have to finish it right now.

    I'm new and now I have to learn as much as possible at same time. I don't really have good basic knowledge about it. Now i'm learning.


    ***Moderate Comment:  Everyone here helps on their free time, and it's Christmas Day in parts of the world.  It is not appropriate for your manager or work or whomever to assign you something you have no experience with and then for you to expect people to figure out your work for you on a free forum.  Please respect this when looking for help and make sure your supervisor knows SharePoint is a huge tool that can't be toyed with.  Assignments like this should go to people who have the proper experience, especially if the work is being charged to a client.

    It's perfectly fine to ask for help, but pressuring people to help you "right now" so that you can finish your real work is way outside the purpose of this forum.


    SharePoint Architect || Microsoft MVP || My Blog
    Planet Technologies || SharePoint Task Force
    Monday, December 26, 2011 3:01 AM
  • Sorry, i'm not English native speaker. Maybe i didn't use correct word. I'm just learning sharepoint, not working, there is no need to finish or make anything done immediately. If you can help me in free time, i highly appreciate, and thanks in advanced. I don't force anyone to complete my work.
    Monday, December 26, 2011 6:23 AM
  • please follow below link which explains step-by-step to get id of task.

    http://blogs.msdn.com/b/tejasr/archive/2008/06/19/how-to-work-around-ontaskcreated-activity-to-get-taskid.aspx

     


    Thanks, Santhosh_SGT
    • Proposed as answer by thanhduc Wednesday, December 28, 2011 3:26 AM
    • Marked as answer by Pengyu Zhao Thursday, December 29, 2011 8:15 AM
    Monday, December 26, 2011 10:13 AM