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)
<configuration>
<appSettings>
<add key=”sitename” value=”http://localhost:4000″/>
</appSettings>
</configuration>
Program.cs
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(“SHAREPOINT USERPROFILE “);
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);
}
else
{
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)
{
DisplayUser(userProfile);
bFound = true;
}
if (username.ToLower().Equals(userProfile[PropertyConstants.UserName].Value.ToString().ToLower()))
{
bFound = true;
Console.WriteLine(“User Found!”);
DisplayUser(userProfile);
}
}
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);
Console.WriteLine(“—————————————–”);
}
}
}