I have been migrating profiles from AD to SharePoint list and created a directory for internal reference.
1) For connecting and migrating data from Active directory use the below code in a webpart on page load:
DirectoryEntry entry;
DirectorySearcher search;
SearchResultCollection results;
try{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite siteCollection = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb site = siteCollection.OpenWeb())
{
entry = GetDirectoryEntry();
search = new DirectorySearcher(entry);
search.Filter = "(&(objectClass=user)(objectCategory=person))";
results = search.FindAll();
this.Controls.Add(new Literal() { Text = "<table border='0' ><tr><td>Name</td><td>Email</td><td>Job Title</td><td>Dept</td><td>Manager</td><td>EMP ID</td><td>Tel No</td><td>Mobile</td><td>Office</td><td>Home</td><td>Desc</td><td>Photo</td></tr>" });
foreach (SearchResult ent in results)
{
string userNameVal = ent.Properties["cn"][0].ToString();
string mailVal = null;
string jobTitleVal = null;
string deptVal = null;
string mgrVal = null;
string empIDVal = null;
string telNoVal = null;
string mobileVal = null;
string officeAddVal = null;
string profilePic = null;
string homeAddrVal = null;
string desc = null;
string nameVal= null;
string dispnameVal = null;
if (ent.Properties["mail"].Count > 0)
{
mailVal = ent.Properties["mail"][0].ToString();
}
if (ent.Properties["title"].Count > 0)
{
jobTitleVal = ent.Properties["title"][0].ToString();
}
if (ent.Properties["department"].Count > 0)
{
deptVal = ent.Properties["department"][0].ToString();
}
if (ent.Properties["manager"].Count > 0)
{
string mng = ent.Properties["manager"][0].ToString();
string[] setp = new string[1];
setp[0] = "OU"; //If your users are in a OU use OU
if (mng.Contains("OU"))
{
mng = mng.Split(setp, StringSplitOptions.None)[0];
mng = mng.Replace("CN=", "");
mng = mng.TrimEnd(',');
mng = mng.Replace("\\, ", ", ");
}
mgrVal = mng;
}
if (ent.Properties["pager"].Count > 0)
{
empIDVal = ent.Properties["pager"][0].ToString();
}
if (ent.Properties["telephoneNumber"].Count > 0)
{
telNoVal = ent.Properties["telephoneNumber"][0].ToString();
}
if (ent.Properties["mobile"].Count > 0)
{
mobileVal = ent.Properties["mobile"][0].ToString();
}
if (ent.Properties["physicalDeliveryOfficeName"].Count > 0)
{
officeAddVal = ent.Properties["physicalDeliveryOfficeName"][0].ToString();
}
if (ent.Properties["thumbnailPhoto"].Count > 0)
{
profilePic = ent.Properties["thumbnailPhoto"][0].ToString();
}
if (ent.Properties["description"].Count > 0)
{
desc = ent.Properties["description"][0].ToString();
}
if (ent.Properties["streetAddress"].Count > 0)
{
homeAddrVal = ent.Properties["streetAddress"][0].ToString();
}
if (ent.Properties["name"].Count > 0)
{
nameVal = ent.Properties["name"][0].ToString();
}
if (ent.Properties["displayName"].Count > 0)
{
dispnameVal= ent.Properties["displayName"][0].ToString();
}
}
string text = string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td><td>{8}</td><td>{9}</td><td>{10}</td><td>{11}</td></tr>",
ent.Properties["cn"][0].ToString(), mailVal, jobTitleVal, deptVal, mgrVal, empIDVal, telNoVal, mobileVal, officeAddVal, homeAddrVal, desc, profilePic);
this.Controls.Add(new Literal() { Text = text });
}
this.Controls.Add(new Literal() { Text = "</table>" });
}
}
});
}
catch (Exception ex)
{
this.Controls.Add(new Literal() { Text = ex.ToString() });
}
Function GetDirectory:
public static DirectoryEntry GetDirectoryEntry()
{
try
{
DirectoryEntry entryRoot = new DirectoryEntry("LDAP://RootDSE");
string Domain = (string)entryRoot.Properties["DefaultNamingContext"][0];
DirectoryEntry de = new DirectoryEntry();
de.Path = "LDAP://" + Domain;
de.AuthenticationType = AuthenticationTypes.Secure;
return de;
}
catch
{
return null;
}
}
2) Another way to directly display records from AD using below code in a webpart on page load:
DirectoryEntry entry;
DirectorySearcher search;
SearchResultCollection results;
protected override void CreateChildControls()
{
try
{
System.Security.PermissionSet ps = new System.Security.PermissionSet(System.Security.Permissions.PermissionState.Unrestricted);
ps.Assert();
Microsoft.SharePoint.SPServiceContext serviceContext = Microsoft.SharePoint.SPServiceContext.Current;
UserProfileManager upm = new Microsoft.Office.Server.UserProfiles.UserProfileManager(serviceContext);
this.Controls.Add(new Literal() { Text = "<table border='0' > <tr><td>Displayname</td><td>Name</td><td>Value</td></tr>" });
ProfileSubtypePropertyManager pspm = upm.DefaultProfileSubtypeProperties;
UserProfile profile = upm.GetUserProfile(SPContext.Current.Web.CurrentUser.LoginName);
foreach (ProfileSubtypeProperty prop in pspm.PropertiesWithSection)
{
if (prop.IsSection)
this.Controls.Add(new Literal() { Text = string.Format("<tr><td colspan='3'><b>Section: {0}</b></td></tr>", prop.DisplayName) });
else
{
string text = string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>",
prop.DisplayName,
prop.Name,
profile[prop.Name].Value);
this.Controls.Add(new Literal() { Text = text });
}
}
this.Controls.Add(new Literal() { Text = "</table>" });
}
catch (Exception ex)
{
this.Controls.Add(new Literal() { Text = ex.ToString() });
}
finally
{
System.Security.CodeAccessPermission.RevertAssert();
}}
You will need to modify the code as your requirements. But hopefully the approach 1 will solve the purpose efficiently.