2

How can I retrieve a non-mapped attribute from active directory (like Birthday), in SharePoint 2013 using C# code?

With "non-mapped" I mean a property not synchronized with the user SharePoint profile

2
  • Do you want the attributes for current user?
    – Hardik
    Jun 20, 2016 at 12:49
  • what i mean is ,i want to retrieve data for those columns of active directory who is not have any mapped attribute,if you wil check then you will find that the birthday is not having this attribute and i want to fetch this value,so how can i achieve this? Jun 20, 2016 at 12:53

2 Answers 2

1

The below code demonstrates how can we fetch Active Directory (AD) using Directory Service. For this, I'm using object array (Users) and here is the structure:

public class Users
{
    public string Email { get; set; }
    public string UserName { get; set; }
    public string DisplayName { get; set; }
    public bool isMapped { get; set; }
}

The code below shows how to fetch user information from Active Directory.

public List<Users> GetADUsers()
{
    try
    {
        List<Users> lstADUsers = new List<Users>();
        string DomainPath = "LDAP://DC=xxxx,DC=com"
        DirectoryEntry searchRoot = new DirectoryEntry(DomainPath); 
        DirectorySearcher search = new DirectorySearcher(searchRoot);
        search.Filter = "(&(objectClass=user)(objectCategory=person))";
        search.PropertiesToLoad.Add("samaccountname");
        search.PropertiesToLoad.Add("mail");
        search.PropertiesToLoad.Add("usergroup");
        search.PropertiesToLoad.Add("displayname");//first name
        SearchResult result;
        SearchResultCollection resultCol = search.FindAll();
        if (resultCol != null)
        {
            for (int counter = 0; counter < resultCol.Count; counter++)
            {
                string UserNameEmailString = string.Empty;
                result = resultCol[counter];
                if (result.Properties.Contains("samaccountname") && 
                         result.Properties.Contains("mail") && 
                    result.Properties.Contains("displayname"))
                {
                    Users objSurveyUsers = new Users();
                    objSurveyUsers.Email = (String)result.Properties["mail"][0] + 
                      "^" + (String)result.Properties["displayname"][0];
                    objSurveyUsers.UserName = (String)result.Properties["samaccountname"][0];
                    objSurveyUsers.DisplayName = (String)result.Properties["displayname"][0];
                    lstADUsers.Add(objSurveyUsers);
                }
            }
        }
        return lstADUsers;
    }
    catch (Exception ex)
    {

}

Above is the sample code to get Mail, UserGroup & Display Name.

If you need all the information than kindly follow steps mentioned below article.

ALL Operations on Active Directory (AD) using c#

4
1

i resolved the issue by applying bellow code
code for retrieving current user

using (SPSite site = new SPSite(SPContext.Current.Web.Url)) ;
        {
            SPUser user = SPContext.Current.Web.CurrentUser;
            var userLoginName = user.LoginName;
            SPServiceContext serviceContext = SPServiceContext.GetContext(site);
            var profileManager = new UserProfileManager(serviceContext);
            var userProfile = profileManager.GetUserProfile(userLoginName);
            var name = userProfile.DisplayName.ToString();
            var BirthDateTime = Convert.ToDateTime(userProfile["SPS-Birthday"].Value).ToLocalTime();
            var HireDate = Convert.ToDateTime(userProfile["SPS-HireDate"].Value).ToLocalTime();
            var dataDiv = new HtmlGenericControl("div");
            var Ul = new HtmlGenericControl("ul");
            var List1= new HtmlGenericControl("li");
            var List2= new HtmlGenericControl("li");
            var List3= new HtmlGenericControl("li");
            List1.InnerText = "Name: " + name.ToString();
            List2.InnerText = "Birthday: " + BirthDateTime;
            List3.InnerText = "Hire date: " + HireDate;
            Ul.Controls.Add(List1);
            Ul.Controls.Add(List2);
            Ul.Controls.Add(List3);
            dataDiv.Controls.Add(Ul); 
            Controls.Add(dataDiv); 
        }


Code to retrieve all users data from current site

using (SPSite site = new SPSite(SPContext.Current.Web.Url))
        {
            SPServiceContext serviceContext = SPServiceContext.GetContext(site);
            UserProfileManager profileManager = new UserProfileManager(serviceContext);
            var dataDiv = new HtmlGenericControl("div");
            var Ul = new HtmlGenericControl("ul");
            foreach (UserProfile profile in profileManager)
            {
                var name = profile.DisplayName.ToString();
                var BirthDateTime = Convert.ToDateTime(profile["SPS-Birthday"].Value).ToLocalTime();
                var HireDate = Convert.ToDateTime(profile["SPS-HireDate"].Value).ToLocalTime();
                var List1= new HtmlGenericControl("li");
                List1.InnerText = "Name: " + name.ToString();
                var List2= new HtmlGenericControl("li");
                List2.InnerText = "Birthday: " + BirthDateTime;
                var List3= new HtmlGenericControl("li");
                List3.InnerText = "Hire date: " + HireDate;
                Ul.Controls.Add(List1);
                Ul.Controls.Add(List2);
                Ul.Controls.Add(List3);
            }                
            dataDiv.Controls.Add(Ul); 
            Controls.Add(dataDiv);
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.