none
Extract all contacts fro Active Directory using VB or C# RRS feed

  • Question

  • Dear Learned colleagues,

                             I have a package that has been running without fail since March 2014. 

                             Part of it's function is to extract all users from AD.

                             I have an addition which needs to be added, and that is I need to also extract all Contacts from AD.

                             I have spent several days on this, tried many googled results but to no avail.

                            Currently I am using the code below to get users from AD, but I now need Contacts and this doesn't work:

    ********************

                                    Dim cntx As New PrincipalContext(ContextType.Domain, sDomainName, sAccount, sPassword)  

                                    Dim gPrincipal As GroupPrincipal = GroupPrincipal.FindByIdentity(cntx, IdentityType.SamAccountName, "Domain Users")

                                    If gPrincipal IsNot Nothing Then

              For Each result As Principal In gPrincipal.GetMembers

                                    ********************

                                    If you have code to provide what I need that would be absolutely splendid.




    • Edited by Mossfree Friday, February 10, 2017 12:19 PM
    Friday, February 10, 2017 12:13 PM

Answers

  • Hi Mossfree,

    welcome to TechNet :)

    Using PowerShell, you could do it with this line:

    Get-ADObject -LDAPFilter "(objectClass=Contact)"

    How it works in VB I know not. I recommend migrating all your VB stuff to PowerShell if possible.

    Cheers,
    Fred

    PS: In C#, you can use a directory searcher (System.DirectoryServices.DirectorySearcher) to do the search. It accepts the same filter string.


    There's no place like 127.0.0.1

    • Marked as answer by Mossfree Saturday, February 11, 2017 10:40 PM
    Friday, February 10, 2017 12:44 PM
  • #1 - this is not a VB forum
    #2 - WE don't fix code on request.
    #3 - Poast in VB forum for assistance.

    P.S. - DirectorySearcher works the same in VB.Net as it does in C# and in PowerShell.

    # PowerShell

    PS C:\scripts> ([adsisearcher]'objectclass=contact').FindAll()
    
    Path                                                 Properties
    ----                                                 ----------
    LDAP://CN=jsmith,OU=TestOU,DC=TESTNET,DC=local     {msexchrecipientdisplaytype, targetaddress, givenname, msexchversion...}
    LDAP://CN=test Contact,OU=TestOU,DC=TESTNET,DC=local {usnchanged, distinguishedname, displayname, whencreated...}
    
    
    PS C:\scripts>


    \_(ツ)_/


    • Edited by jrv Friday, February 10, 2017 2:11 PM
    • Proposed as answer by Richard MuellerMVP, Moderator Friday, February 10, 2017 6:32 PM
    • Marked as answer by Mossfree Saturday, February 11, 2017 10:40 PM
    Friday, February 10, 2017 2:07 PM

All replies

  • Hi Mossfree,

    welcome to TechNet :)

    Using PowerShell, you could do it with this line:

    Get-ADObject -LDAPFilter "(objectClass=Contact)"

    How it works in VB I know not. I recommend migrating all your VB stuff to PowerShell if possible.

    Cheers,
    Fred

    PS: In C#, you can use a directory searcher (System.DirectoryServices.DirectorySearcher) to do the search. It accepts the same filter string.


    There's no place like 127.0.0.1

    • Marked as answer by Mossfree Saturday, February 11, 2017 10:40 PM
    Friday, February 10, 2017 12:44 PM
  • #1 - this is not a VB forum
    #2 - WE don't fix code on request.
    #3 - Poast in VB forum for assistance.

    P.S. - DirectorySearcher works the same in VB.Net as it does in C# and in PowerShell.

    # PowerShell

    PS C:\scripts> ([adsisearcher]'objectclass=contact').FindAll()
    
    Path                                                 Properties
    ----                                                 ----------
    LDAP://CN=jsmith,OU=TestOU,DC=TESTNET,DC=local     {msexchrecipientdisplaytype, targetaddress, givenname, msexchversion...}
    LDAP://CN=test Contact,OU=TestOU,DC=TESTNET,DC=local {usnchanged, distinguishedname, displayname, whencreated...}
    
    
    PS C:\scripts>


    \_(ツ)_/


    • Edited by jrv Friday, February 10, 2017 2:11 PM
    • Proposed as answer by Richard MuellerMVP, Moderator Friday, February 10, 2017 6:32 PM
    • Marked as answer by Mossfree Saturday, February 11, 2017 10:40 PM
    Friday, February 10, 2017 2:07 PM