SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a Custom List, which have different fields, when I click on any user in field "assignedTo" it navigates me to another window and I can see user's Account, Name, email etc.

I am able to get all the items of the list and of that specific field but i dont know how can i get user's email out of it. am not sure if it's sharepoint thing or we made it custom. here's the code i made until now

using (SPSite site = new SPSite("www.local.com"))
    using (SPWeb web = site.OpenWeb())
    {
        SPList mySourceList = web.Lists["ListName"];

        SPQuery mySourceListQuery = new SPQuery();

        mySourceListQuery.Query =

            "<OrderBy><FieldRef Name='AssignedTo' />" +
                     "<FieldRef Name='Title' />" +
            "</OrderBy>";

        SPListItemCollection mySourceItemColl = mySourceList.GetItems(mySourceListQuery);
                foreach (SPListItem mySourceListItem in mySourceItemColl)
            {
                if (!UserDetails.ContainsKey(mySourceListItem["AssignedTo"].ToString()))
                {
                    string name;
                    name = mySourceListItem["AssignedTo"].ToString();

                    name = name.Substring(name.IndexOf("#") + 1);


                    UserDetails.Add(name, mySourceListItem["Title"].ToString());
                }
            }

            foreach (SPUser user in web.Users)
            {
                if (UserDetails.ContainsKey(user.Name))
                    Console.WriteLine(user.Email);
            }
        }
    }
share|improve this question
up vote 4 down vote accepted

Try this

SPFieldUser assignedto = mySourceList.Fields["AssignedTo"] as SPFieldUser;
...
...
foreach(SPListItem ...)
{
  if(!UserDetails...)
    {
        SPFieldUserValue assignedUser = assignedto.GetFieldValue(mySourceListItem["AssignedTo"].ToString());
        if(assignedUser != null)
           {
              string name = assignedUser.User.DisplayName;
              string loginName = assignedUser.User.LoginName;
              string email = assignedUser.User.Email;
           }

    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.