SharePoint Learning
Login
Signup Free
User Login
Close
 
 
Keep me logged in on this computer
Signup for a FREE user account
Close
Email
Password
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?

Namespace

using Microsoft.SharePoint;

Code

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

 

Comments:

No comments.

Copyright © SharePointLearning.net.au. All Rights Reserved | Contact Us

Powered by DigiLife Studio