1

I want to retrieve date of joining (hire date) and date of birth (Birthdate) of every user from Active directory. How can I achieve this? i tried something like

using (var context = new PrincipalContext(ContextType.Domain, "yourdomain.com"))
{
    using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
    {
        foreach (var result in searcher.FindAll())
        {
            DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
            Console.WriteLine("First Name: " + de.Properties["givenName"].Value);
            Console.WriteLine("Last Name : " + de.Properties["sn"].Value);
            Console.WriteLine("SAM account name   : " + de.Properties["samAccountName"].Value);
            Console.WriteLine("User principal name: " + de.Properties["userPrincipalName"].Value);
            Console.WriteLine();
        }
    }
}
Console.ReadLine();


but here it is only working with mapping attribute name,rather internal or display name,and this birth date and hire date column doesn't have mapping attribute name,so how can i get rid of this?

3 Answers 3

2

try the bellow code that is for retrieving current users data

include the name spaces

using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;


apply the following code

       using (SPSite spsite = new SPSite(SPContext.Current.Web.Url)) ;
            {
                SPUser spuser = 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();
                Console.WriteLine("Name: " + name.ToString());
                Console.WriteLine("Birthday: " + BirthDateTime);
                Console.WriteLine("Hire date: " + HireDate);
            }


and check the bellow code for retrieving all users for current site

       using (SPSite site = new SPSite(SPContext.Current.Web.Url))
            {
                SPServiceContext serviceContext = SPServiceContext.GetContext(site);
                UserProfileManager profileManager = new UserProfileManager(serviceContext);
                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();
                    Console.WriteLine("Name: " + name.ToString());
                    Console.WriteLine("Birthday: " + BirthDateTime);
                    Console.WriteLine("Hire date: " + HireDate);
                }
            }
1

User's Properties from Active Directory can be achieved through profile synchronization.

  • In Central Administration, verify settings on user profile service synchronization properties
  • Run “User profile service synchronization" service. This will update all the user profile information

With the help of CSOM and REST API you can perform an activity to retrieve Hire date and birthday. Internal name of hire date: “SPS-HireDate” and birthday : “SPS-Birthday”

Example:

    var userProfileProperties;
// Ensure that the SP.UserProfiles.js file is loaded before the custom code runs.

SP.SOD.executeOrDelayUntilScriptLoaded(getUserProperties, 'SP.UserProfiles.js');

function getUserProperties() {
    // Replace the placeholder value with the target user's credentials.
    var targetUser = "domainName\\userName";

    // Get the current client context and PeopleManager instance.
    var clientContext = new SP.ClientContext.get_current();
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);

    // Specify the properties to retrieve and target user for the 
    // UserProfilePropertiesForUser object.
    var profilePropertyNames = ["SPS-HireDate", "SPS-Birthday"];
    var userProfilePropertiesForUser = 
        new SP.UserProfiles.UserProfilePropertiesForUser(
            clientContext,
            targetUser,
            profilePropertyNames);

    // Get user profile properties for the target user.
    // To get the value for only one user profile property, use the
    // getUserProfilePropertyFor method.
    userProfileProperties = peopleManager.getUserProfilePropertiesFor(
        userProfilePropertiesForUser);

    // Load the UserProfilePropertiesForUser object and send the request.
    clientContext.load(userProfilePropertiesForUser);
    clientContext.executeQueryAsync(onRequestSuccess, onRequestFail);
}

// This function runs if the executeQueryAsync call succeeds.
function onRequestSuccess() {
    var messageText = "\"Hire date \" property is " 
        + userProfileProperties[0];
    messageText += "<br />\"DOB\" property is " 
        + userProfileProperties[1];
    $get("results").innerHTML = messageText;
}

// This function runs if the executeQueryAsync call fails.
function onRequestFail(sender, args) {
    $get("results").innerHTML = "Error: " + args.get_message();
}

Reference:

http://www.c-sharpcorner.com/UploadFile/anavijai/how-to-get-a-particular-users-properties-using-csom-in-shar/

https://msdn.microsoft.com/en-us/library/office/jj920104.aspx http://nikhilsarvaiye.blogspot.in/2013/09/sharepoint-2013-working-with-user.html https://bramdejager.wordpress.com/2011/02/03/default-user-profile-properties/

2
0

You need to know Active directory programming with C#. Following reference is very helpful.

Howto: (Almost) Everything In Active Directory via C#

This section will help you to read user properties like joining date and etc.

2

Your Answer

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

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