Get SPUser or SPGroup by AssignedToID

string AssignedTo = Convert.ToString(item[“Assigned To”]);
int assignedToID = Convert.ToInt32(AssignedTo.Substring(0,AssignedTo.IndexOf(“;#”)));

string userDisplayName = “”;
string userEmail = “”;

SPUser spUser = new SPFieldUserValue(currentWeb, Convert.ToInt32(assignedToID), null).User; // get User by ID
if (spUser != null) // It is a user
{
userEmail = spUser.Email;
userDisplayName = spUser.Name;
}
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);
}

More details: http://www.sharepointlearning.net.au/sharepoint_article_details.aspx?articleID=15&title=Get%20%20the%20email%20address%20for%20the%20user%20(or%20group)%20who%20is%20in%20the%20

Advertisements
This entry was posted in SharePoint 2010, Visual Studio 2010. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s