Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8527303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:31:11+00:00 2026-06-11T08:31:11+00:00

I need to provide a search on Active Directory using filters such as Display

  • 0

I need to provide a search on Active Directory using filters such as Display name, Telephone and Department. Display name and Telephone are easy but I’m stuck on department. This is the bit that works:

using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
{
    UserPrincipal userPrincipal = new UserPrincipal(context);

    if (txtDisplayName.Text != "")
        userPrincipal.DisplayName = "*" + txtDisplayName.Text + "*";

    using (PrincipalSearcher searcher = new PrincipalSearcher(userPrincipal))
    {
        foreach (Principal result in searcher.FindAll())
        {
           DirectoryEntry directoryEntry = result.GetUnderlyingObject() as DirectoryEntry;
           DataRow drName = dtProfile.NewRow();
           drName["displayName"] = directoryEntry.Properties["displayName"].Value;
           drName["department"] = directoryEntry.Properties["department"].Value;
           dtProfile.Rows.Add(drName);
        }
    }
} 

I was hoping that I could just add something like:

DirectoryEntry userDirectoryEntry = userPrincipal.GetUnderlyingObject() as DirectoryEntry;
if (ddlDepartment.SelectedValue != "")
    userDirectoryEntry.Properties["title"].Value = ddlDepartment.SelectedValue;

But that doesn’t work. Anyone know how I can do this?

Edit:
I’m an idiot, changed the search term and found the answer. The extra fields are called attibutes. Thanks Raymund Macaalay for your blog article on extending Principals.

My extended UserPrincipal:

[DirectoryObjectClass("user")]
[DirectoryRdnPrefix("CN")]

public class UserPrincipalExtended : UserPrincipal
{    
    public UserPrincipalExtended(PrincipalContext context) : base(context) 
    {
    }
    [DirectoryProperty("department")]
    public string department
    {
        get
        {
            if (ExtensionGet("department").Length != 1)
                return null;
            return (string)ExtensionGet("department")[0];
        }
        set { this.ExtensionSet("department", value); }
    }
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T08:31:13+00:00Added an answer on June 11, 2026 at 8:31 am

    Since you’ve already extended the UserPrincipal to include the Department attribute, you’ll need to use that extended version of the user principal when you want to search.

    Try this:

    using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
    {
        UserPrincipalExtended userPrincipal = new UserPrincipalExtended(context);
    
        if (txtDisplayName.Text != "")
        {
            userPrincipal.DisplayName = "*" + txtDisplayName.Text + "*";
        }
    
        if (!string.IsNullOrEmpty(txtDepartment.Text.Trim())
        {
            userPrincipal.department = txtDepartment.Text.Trim();
        }
    
        using (PrincipalSearcher searcher = new PrincipalSearcher(userPrincipal))
        {
            foreach (Principal result in searcher.FindAll())
            {
               UserPrincipalExtended upe = result as UserPrincipalExtended;
    
               if (upe != null)
               {
                   DataRow drName = dtProfile.NewRow();
                   drName["displayName"] = upe.DisplayName;
                   drName["department"] = upe.department;
                   dtProfile.Rows.Add(drName);
               }
            }
        }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project using Lucene3.5 already. Now i need to provide web search
I'm trying to search on a table using full-text search but i need to
I need to put together a data structure that will efficiently provide keyword search
I need to provide our web developers an easy and quick way to test
I need to provide some kind of global search over most of the data
I am using a pagination plugin called pajinate, i need to provide a Show
I need to provide full text search on javascript source files and highlighting of
In an iOS app, I need to provide image filters based on their size
I need to create a simple text file based search engine asap (using PHP)!
I am using Apache Solr for search. I use this to provide personal user-based

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.