SharePoint Learning
Signup Free
User Login
Keep me logged in on this computer
Signup for a FREE user account
Designation (optional)
First Name
Last Name
Headline (optional)
Online SharePoint training videos and articles
Price: FREE

Get the email address for the user (or group) who is in the "Assigned To" field of a task list programmatically

By Lin Zhang on 20 June 14

We know the ID of the user (or group) in the "Assigned To" field of a task list. How can we get their email addresses by this user ID or group ID?


using Microsoft.SharePoint;


using (SPSite currentSite = new SPSite(SPContext.Current.Site.ID))
            using (SPWeb currentWeb = currentSite.OpenWeb("crm"))
                SPListItemCollection listItems_Tasks = currentWeb.Lists["Tasks"].Items;
                foreach (SPListItem item in listItems_Tasks)
                    string AssignedTo = Convert.ToString(item["Assigned To"]);
                    int assignedToID = Convert.ToInt32(AssignedTo.Substring(0, AssignedTo.IndexOf(";#")));

                    string userEmail = "";

                    SPUser spUser = new SPFieldUserValue(currentWeb, assignedToID, null).User; // get User by ID
                    if (spUser != null) // It is a user
                        userEmail = spUser.Email;
                    else  // it is a Group
                        SPGroup spGroup = currentWeb.SiteGroups.GetByID(assignedToID);
                        foreach (SPUser user in spGroup.Users)
                            userEmail += user.Email + ";";
                        userEmail = userEmail.Substring(0, userEmail.Length - 1);



No comments.

Copyright © All Rights Reserved | Contact Us

Powered by DigiLife Studio