2

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

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

    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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