Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

1 Answer 1

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.