Extract Email ID and his Managers Email ID using Employee Number.
Thanks for the Solution @Sweta Singh
🚀 Check out this powerful code snippet for Active Directory user retrieval! 🌐
Are you looking to streamline your automation processes? This code is a game-changer. It efficiently fetches user and manager details from the Active Directory, ensuring seamless data retrieval for your automation tasks.
🔍 With meticulous handling for user existence, error management, and retrieval of manager details, this code snippet sets the standard for precise, error-free automation.
Discover the efficiency of user and manager data extraction and elevate your automation game with this expertly crafted code!
Initailization :


Code:
success = false;
message = "An error occurred."; // Default error message
EmailID = "";
ManagerEmailID = "";
exist = false;
try
{
using (PrincipalContext context = new PrincipalContext(ContextType.Domain, AD_DomainName))
{
using (UserPrincipal user = UserPrincipal.FindByIdentity(context, System.DirectoryServices.AccountManagement.IdentityType.SamAccountName, userName))
{
if (user != null)
{
exist = true;
EmailID = user.EmailAddress;
// Retrieve the user's manager
if (user.GetUnderlyingObject() is DirectoryEntry directoryEntry && directoryEntry.Properties.Contains("manager"))
{
string managerDistinguishedName = directoryEntry.Properties["manager"].Value?.ToString();
// Fetch manager details by distinguished name
if (!string.IsNullOrEmpty(managerDistinguishedName))
{
using (UserPrincipal manager = UserPrincipal.FindByIdentity(context, System.DirectoryServices.AccountManagement.IdentityType.DistinguishedName, managerDistinguishedName))
{
if (manager != null)
{
ManagerEmailID = manager.EmailAddress;
success = true; // Update success flag upon successful retrieval
message = ""; // Clear error message on success
}
}
}
}
}
else
{
// Handle case when user doesn't exist
message = "User not found in Active Directory.";
}
}
}
}
catch (Exception e)
{
message = e.Message; // Update error message with the exception details
// Log the exception or handle it as needed
}
Action:

#BluePrism #AutomationDeveloper #CodeEfficiency #ActiveDirectory #AutomationExperts
Uzer Shaikh | Consultant – Automation Developer
Wonder Botz Pvt Ltd.