Dotnetdreamer's Weblog

SharePoint, Silverlight and Azure

Accessing User Profiles in MOSS 2007

Posted by Ramprasad Navaneethakrishnan on October 17, 2008

On more than one occation, there arouse a need to access the user profile information of the Sharepoint site.The ‘UserProfileManager’ class present in Microsoft.Office.Server.UserProfile holds all the user profiles present in a sharepoint site. All we need to do is to pass the ‘ServerContext’ object as parameter to this UserProfileManager’s constructor.

I have pasted the code of my console application which basically does the following two things..

1. List all the user profiles for the site.

2. Searches the user profile for a specific user.

Here is the code.

App.Config (change the sitename value that matches your site)

<?xml version=”1.0″ encoding=”utf-8″ ?>



<add key=”sitename” value=”http://localhost:4000″/&gt;




using System;

using System.Configuration;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Web;

using Microsoft.Office.Server.Administration;

using Microsoft.Office.Server.UserProfiles;

using Microsoft.SharePoint;

using Microsoft.Office.Server;

namespace UserProfilerCApp


class Program


static void Main(string[] args)


while (true)



Console.WriteLine(“1.To List all users Press 1 “);

Console.WriteLine(“2.To search user Press 2 “);

Console.Write(“Enter your option : “);

int isSearch = Convert.ToInt32(Console.ReadLine());

AppSettingsReader appreader = new AppSettingsReader();

string siteName = appreader.GetValue(“sitename”, Type.GetType(“System.String”)).ToString();

if (isSearch == 2)


Console.WriteLine(“Enter the username to be searched”);

string username = Console.ReadLine();

SearchUser(siteName, username, true);




SearchUser(siteName, string.Empty, false);




private static UserProfileManager GetAllUserProfile(string sitename)


using (SPSite site = new SPSite(sitename))


ServerContext context = ServerContext.GetContext(site);

UserProfileManager profileManager = new UserProfileManager(context);

return profileManager;



private static void SearchUser(string sitename, string username,bool search)


bool bFound = false;

UserProfileManager profileManager = GetAllUserProfile(sitename);

foreach (Microsoft.Office.Server.UserProfiles.UserProfile userProfile in profileManager)


if (!search)



bFound = true;


if (username.ToLower().Equals(userProfile[PropertyConstants.UserName].Value.ToString().ToLower()))


bFound = true;

Console.WriteLine(“User Found!”);




if (!bFound)


Console.WriteLine(“No User Found!”);



private static void DisplayUser(UserProfile userProfile)


Console.WriteLine(PropertyConstants.UserName + ” : ” + userProfile[PropertyConstants.UserName].Value);

Console.WriteLine(PropertyConstants.AccountName + ” : ” + userProfile[PropertyConstants.AccountName].Value);

Console.WriteLine(PropertyConstants.Department + ” : ” + userProfile[PropertyConstants.Department].Value);

Console.WriteLine(PropertyConstants.FirstName + ” : ” + userProfile[PropertyConstants.FirstName].Value);

Console.WriteLine(PropertyConstants.WorkPhone + ” : ” + userProfile[PropertyConstants.WorkPhone].Value);






4 Responses to “Accessing User Profiles in MOSS 2007”

  1. […] Accessing User Profiles in MOSS 2007 […]

  2. After reading

  3. This is a very usefull code. The problem I am facing with this is that, I have 20k users and this code takes around 4 minutes to search the results. Is there any faster way to do this?

  4. I have used same code in my webpart, My issue is latter i have added one custom field into ssp userprofile ipPhone mapped to Ldap. that field i didn’t got into my c# application like “userProfile[PropertyConstants.??????(i want here ipPhone field that i had added into ssp userprofile)].Value);” Can you please suggest is there any update that i have to perform.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: