I’ve created a List with a ‘Person’ column to manage favourite people. So the operator will add a few favourite people to the list. It appears that the person column can only return a single attribute. Then I want to display the favourite people on the home page with other details like phone number, email address. So I guess I need to create a webpart that reads the list and then fetches the email address and phone number from user profile and finally displays the info. I’ve been reading that SharePoint also has a hidden list (_layouts/users/details.aspx) but this only gets populated once a user accesses the site, so this wont work. Hence I think User Profiles is the option here. Any ideas how I can achieve this? Thanks

  • just a heads up. do not use the user information hidden list for this because there are many complications in that. Even if you delete a user from your AD, the user information list will not be updated. therefore your webpart will not show the correct information. Jan 7, 2015 at 9:50

1 Answer 1


Go to Visual Studio and create a new project, look under SharePoint and click on the version you're using. I'm presuming 2010. You should see some options and for you, you can click on Visual Webpart. Give it a meaningful name and click ok.

Go to Solution Explorer and then expand VisualWebPart1 and double click on the .cs file to start coding. The ascx is the front end (visual) part where you can add controls ect. If you expand that you have a codebehind for that ascx where you can also add code, but in this case we're not adding code within the ascx but rather inheriting by adding it to the .cs codebehind.

The following code has not been tested and I just made it so more than likely would need fine tuning or amending to work properly! You need to amend the list name that you want to pull out to your one. I am missing some checks for the user that you might want to do to see if they exist rather than throwing a not found exception!

As noted it's not properly done so could be cut down and made better. The getUser method could be expanded to cut the main body code down so that it returns a list of data rather than the spuser object but I will leave that for you.

    // Visual Studio might automatically update this path when you change the Visual Web Part project item.
    private const string _ascxPath = @"~/_CONTROLTEMPLATES/showUsers/VisualWebPart1/VisualWebPart1UserControl.ascx";
    private UserProfileManager upm;
    protected override void CreateChildControls()
        Control control = Page.LoadControl(_ascxPath);

        //get your custom list and populate all the items into a collection to iterate over
        SPList myFavList = SPContext.Current.Web.Lists["myFavList"];
        SPListItemCollection items = myFavList.Items;

        upm = new UserProfileManager(SPServiceContext.Current);

        //list view to contain all the values and to display on the webpart!
        Microsoft.SharePoint.WebControls.ListView lv = new Microsoft.SharePoint.WebControls.ListView();

        Random rnd = new Random();

        //iterate through each list item
        foreach (SPListItem item in items)
            //get the user from the list item
            //I don't know what your list looks like so I'm presuming that you have a list of users(people) that you want to iterate to get their details from????
            SPUser user = getUser(item["people"].ToString());

            if (user != null)
                //make sure its not a service account or system account
                if (user.LoginName.ToString() != "NT AUTHORITY\\LOCAL SERVICE" && user.LoginName.ToString() != "SHAREPOINT\\system")
                    //get the profile object for that user
                    UserProfile profile = upm.GetUserProfile(user.LoginName);

                    //display the data to page any way you like!!:

                    Label lbl = new Label();
                    lbl.ID = rnd.Next().ToString();
                    //an example of what it would look like: "ali sharepoint's workemail : [email protected]"
                    //you can do what you like, this example is wacking the data into a label which isnt great. You can create its visual container within the ascx or change the label to a gridview or table
                    lbl.Text = user.LoginName + "'s workemail : " + (string)profile["WorkEmail"].Value;

    //getUser needs to be domain\username
    private SPUser getUser(string getUser)
        SPUser user = null;
            // Chek if user exists
            if (upm.UserExists(getUser))
                // The user exists
                user = SPContext.Current.Web.AllUsers[getUser];
                // The User doesn't exists

        catch(Exception a)
            //user doesn't exist// catch exception here and carry on

        return user;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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