prog1011
prog1011

Reputation: 3495

read Parent-GUID attribute from ActiveDirectory using C#

I want to read Parent-GUID attribute from ActiveDirectory.

I have tried below code to read all attributes of AD object from ActiveDirectory.

Code

var dirEntry = new DirectoryEntry(directoryEntryPath);
var directorySearcher = new DirectorySearcher(dirEntry, filter)
{
    CacheResults = false,
    Tombstone = true,                
};
var searchResult = directorySearcher.FindAll(); // get mutiple AD Objects
foreach (SearchResult search in searchResult)
{
    foreach (DictionaryEntry prop in search.Properties) // here I get all attributes values  But not able to find parent-GUID attribute
    {

    }
}

Using above code I am able to get all properties of AD Object but I am not able to get value of Parent-GUID attribute.

Upvotes: 0

Views: 631

Answers (3)

peacev
peacev

Reputation: 1

var searchResult = directorySearcher.FindAll();
foreach(SearchResult search in searchResult)
{
    DirectoryEntry de = search.GetDirectoryEntry();
    Guid ParentGUID = new Guid((byte[])de.Parent.Properties["objectGUID"][0]);
    ...
}

Upvotes: 0

Brian Desmond
Brian Desmond

Reputation: 4503

According to https://learn.microsoft.com/en-us/windows/desktop/adschema/a-parentguid this is a constructed attribute. This means it won't be included in search results. The docs also imply it's there to support DirSync which tells me that it might not be available outside of a DirSync search.

Upvotes: 1

ov4rlrd
ov4rlrd

Reputation: 33

Do you mean something like that?:

string path = "CN=someone,OU=yourOrganizationalUnit,DC=example,DC=com";
DirectoryEntry root = new DirectoryEntry(path);
root.Parent.Guid.ToString(); // this will display you the GUID from the parent of your path

Hope this is what you meant!

Cheers,
ov4rlrd

Upvotes: 0

Related Questions