locked
Active Directory sort propertyname RRS feed

  • Question

  • User799222684 posted

    I am getting many properties for the AD record in C#. I list out these values as ( partial list below)

    samaccounttype = 8053

    lastlogon = 131692205645025544

    homedrive = C:

    displayname = Steve

    cn = me

    ... etc

    I would like to sort on property name.  so the output would look like this:

    cn = me

    displayname = Steve

    homedrive = C:

    lastlogon = 131692205645025544

    samaccounttype = 8053

    etc

    I see how to sort on example "displayname". Not sure how sort on ALL property name.

    SortOption sortedResults = new SortOption();
    sortedResults.PropertyName = "displayname"; 

    TIA

    Steve42

    Thursday, April 26, 2018 2:34 PM

Answers

  • User283571144 posted

    Hi sh,

    As far as I know, there are no made-in method to sort the property name.

    If you want to sort the property by property name, I suggest you could use directory to achieve your requirement.

    You could firstly read the property name as directory name and the value as the directory value.

    Then you could use convert the directory key to list and use Sort method to sort the value.

    More details, you could refer to below codes:

                SearchResult result = search.FindOne();
    
                var re = result.Properties.PropertyNames;
    
                var dictionary = new Dictionary<string, string>();
    
                foreach (var item in re)
                {
                    dictionary.Add((string)item, (String)result.Properties[(string)item][0]);
                }
    
                var list = dictionary.Keys.ToList();
                list.Sort();
    
                // Loop through keys.
                foreach (var key in list)
                {
                    Console.WriteLine("{0}: {1}", key, dictionary[key]);
                }   
                Console.ReadLine();

    Best Rregards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 27, 2018 5:41 AM

All replies

  • User283571144 posted

    Hi sh,

    As far as I know, there are no made-in method to sort the property name.

    If you want to sort the property by property name, I suggest you could use directory to achieve your requirement.

    You could firstly read the property name as directory name and the value as the directory value.

    Then you could use convert the directory key to list and use Sort method to sort the value.

    More details, you could refer to below codes:

                SearchResult result = search.FindOne();
    
                var re = result.Properties.PropertyNames;
    
                var dictionary = new Dictionary<string, string>();
    
                foreach (var item in re)
                {
                    dictionary.Add((string)item, (String)result.Properties[(string)item][0]);
                }
    
                var list = dictionary.Keys.ToList();
                list.Sort();
    
                // Loop through keys.
                foreach (var key in list)
                {
                    Console.WriteLine("{0}: {1}", key, dictionary[key]);
                }   
                Console.ReadLine();

    Best Rregards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 27, 2018 5:41 AM
  • User799222684 posted

    THANKS! That was a big help.

    Sunday, April 29, 2018 4:42 PM