Tuesday, September 27, 2011

Get UserProfile using UserProfileManager in SharePoint and ASP.NET application

Fetch Contact details from Active directory and SharePoint contacts using User profile service


public ArrayList SearchingFromUserProfile(string key)
{
//getting the URL of Central Admin.
Microsoft.SharePoint.Administration.SPAdministrationWebApplication centralWeb = SPAdministrationWebApplication.Local;
string centraladmin = centralWeb.GetResponseUri(Microsoft.SharePoint.Administration.SPUrlZone.Custom).AbsoluteUri.ToString();

ArrayList result = new ArrayList();

//Initallizing the UserProfileManager.
using (SPSite site = new SPSite(centraladmin))
{
SPServiceContext context = SPServiceContext.GetContext(site);
UserProfileManager userprofilemanager = new UserProfileManager(context);

//Getting WorkEmail of all the Users Resolved.
foreach (UserProfile profile in userprofilemanager)
{
object property = profile[PropertyConstants.WorkEmail];
if(property != null && property != "")
result.Add(profile[PropertyConstants.WorkEmail].ToString());
}
}
return result;
}