Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
C#
//This method runs when the button is clicked

you should use the references
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;

    void runQueryButton_Click(object sender, EventArgs e)
    {
        //Get the My Sites site collection, ensuring proper disposal
        using (SPSite mySitesCollection = new SPSite("http://sp/my"))
        {
            //Get the user profile manager
            SPServiceContext context = SPServiceContext.GetContext(mySitesCollection);
            UserProfileManager profileManager = new UserProfileManager(context);
            //How many user profiles are there?
            resultsLabel.Text = "There are " + profileManager.Count + " user profiles.<br />";
            //Loop through all the user profiles
            foreach (UserProfile currentProfile in profileManager)
            {
                //There is always a display name
                resultsLabel.Text += "User: " + currentProfile.DisplayName + "<br />";
                //Be careful to avoid null errors
                if (currentProfile["Department"].Value != null)
                {
                    //There is a department listed so display it
                    resultsLabel.Text += "Department: " + currentProfile["Department"].Value.ToString() + "<br /><br />";
                }
                else
                {
                    resultsLabel.Text += "Department: None<br /><br />";
                }
            }
        }
    }
Posted
Updated 14-Dec-14 20:22pm
v3
Comments
PIEBALDconsult 14-Dec-14 16:27pm    
Did you have a question? Use Improve question to add detail. Please include where the classes SPSite, SPServiceContext, etc. can be found.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900