none
Retrieving properties from Active Directory offline RRS feed

  • Question

  • Hello,
    I have some code a web developer colleague gave me to retrieve a property value out of Active Directory for the logged on user.  
    Private Function getADProperty(ByVal samAccountName As String, ByVal propertyname As String, ByVal path As String) As String
            Try
                Using de As New DirectoryEntry(path, Nothing, Nothing, AuthenticationTypes.Secure)
                    Dim search As New DirectorySearcher(de)
                    Dim result As SearchResult
                    search.Filter = ("(&(objectCategory=person)(objectClass=user)(samaccountname=" & samAccountName & "))")
                    result = search.FindOne()
                    If IsNothing(result) Then
                        Return String.Empty
                    Else
                        Dim properties As ResultPropertyCollection = result.Properties
                        If properties(propertyname).Count > 0 Then
                            Return properties(propertyname)(0).ToString()
                        Else
                            Return String.Empty
                        End If
                    End If
                End Using
            Catch ex As Exception
                Return String.Empty
            End Try
        End Function
    Where:
    path = "GC://DOMAINNAME"
    samAccountName = System.Environment.UserName 
    propertyname = "physicaldeliveryofficename"
    All good when the user is online but I also need to get to this data for laptop users when they are offline. I thought at least some AD data is cached locally so hunted around and tried this alternative search path: 
    "WinNT://" & Environment.MachineName
    or 
    "WinNT://" & Environment.MachineName & ",computer"
    but no luck. When offline, these search paths both caused exceptions on the line: 
    result = search.FindOne()
    "The provider does not support searching and cannot search WinNT://<MACHINENAME>."
    or
    "The provider does not support searching and cannot search WinNT://<MACHINENAME>,computer."
    Is it possible to get this data locally or do I need to find another way to source the user's office?
    Thanks in advance for any help.
    Steve
    • Moved by Paul Zhou Tuesday, July 19, 2011 7:48 AM (From:.NET Base Class Library)
    Wednesday, July 13, 2011 11:46 PM

All replies

  •  

    Hi Steve,

     

    Welcome to the MSDN forum!

     

    According to your description, I'd like to move your thread to off-topic because it is not a problem related to this forum. You may post thread in General Windows Development Issues. There are more experts in that forum so that you may get more useful help there.

     

    Thank you for your posting and understanding.

    Have a nice day!


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, July 19, 2011 7:47 AM