Link to home
Start Free TrialLog in
Avatar of Dave McConnell
Dave McConnellFlag for United States of America

asked on

DS datatype conversion

How can I store the userPrincipalName value from an AD search into a textbox.  When I try to set TextBox1.Text = resEnt.GetDirectoryEntry().Properties("givenName").Value() it is no problem.  However, if I try TextBox1.Text = resEnt.GetDirectoryEntry().Properties("userPrincipalName").Value() then I receive an error about "The directory datatype cannot be converted to/from a native DS datatype".
Avatar of graye
graye
Flag of United States of America image

If I remember correctly, the userPrincipalName is not a required field in Active Directory.

So, most likely, there's nothing wrong with your code (other than it doesn't check to see if the userPrincipalName attribute exists)
Avatar of Dave McConnell

ASKER

I should have included the code:

        Dim sourcename As String

        Dim dirEntry As DirectoryEntry
        Dim ldapPath = "LDAP://DC=xxx,DC=ds,DC=xxx"
        Dim enTry As New DirectoryEntry(ldapPath, "", "")

        Dim filter As String = String.Format("(&(objectCategory=user)(givenName={0})(sn={1}))", TextBoxFirstName.Text, TextBoxLastName.Text)

        Dim searcher As New DirectorySearcher(enTry, filter)
        Dim resEnt As SearchResult

        '        Console.WriteLine("Search Results = " & searcher.FindAll().ToString())

        For Each resEnt In searcher.FindAll()
            searcher.PropertiesToLoad.Add("userPrincipalName")
        Next
        TextBoxUPN.Text = resEnt.GetDirectoryEntry().Properties("mail").Value()



If I change the Properties value on the last line to either sAMAccountName or UserPrincipalName I will receive the same error about "datatype cannot be converted", however I know sAMAccountName must be included in AD.

Does anyone have an answer as this is getting to be critical?

Thanks,

-Dave
You should move the PropertiesToLoad method up a few lines... it should appear before you execute the FindAll method

Inside the For Each loop, you should retrieve the property like this:

           if resEnt.Properties("UserPrincipalName").Count > 0 then
                SomeText = resEnt.Properties("UserPrincipalName").Value.ToString
           end if
=======================================================================
searcher.PropertiesToLoad.Add("userPrincipalName")
For Each resEnt In searcher.FindAll()
   If resEnt.Properties("userPrincipalName").Count > 0 Then
      TextBoxUPN.Text = resEnt.GetDirectoryEntry().Properties("userPrincipalName").Value().ToString
   End If
Next
=======================================================================
This gives me the same error about "datatype cannot be converted".


=======================================================================
searcher.PropertiesToLoad.Add("userPrincipalName")
For Each resEnt In searcher.FindAll()
   If resEnt.Properties("userPrincipalName").Count > 0 Then
      TextBoxUPN.Text = resEnt.Properties("userPrincipalName").Value.ToString
   End If
Next
=======================================================================
When I try to compile I get:  "MainForm.vb(185): 'Value' is not a member of 'System.DirectoryServices.ResultPropertyValueCollection'."


=======================================================================
searcher.PropertiesToLoad.Add("userPrincipalName")
For Each resEnt In searcher.FindAll()
   If resEnt.Properties("userPrincipalName").Count > 0 Then
      TextBoxUPN.Text = resEnt.Properties("userPrincipalName").ToString
   End If
Next
=======================================================================
If I remove ".value" it will compile and run, but I get this in the textbox:  "System.DirectoryServices.ResultPropertyValueCollection".


Still need some help here...



ASKER CERTIFIED SOLUTION
Avatar of graye
graye
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I receive this error: "datatype cannot be converted" on the "if resEnt.GetDi....."  line.

here is the code again:

=====================================================
        Dim sourcename As String

        Dim dirEntry As DirectoryEntry
        Dim ldapPath = "LDAP://DC=xxx,DC=ds,DC=yyy,DC=gov"
        Dim enTry As New DirectoryEntry(ldapPath, "", "")

        Dim filter As String = String.Format("(&(objectCategory=user)(givenName={0})(sn={1}))", TextBoxFirstName.Text, TextBoxLastName.Text)

        Dim searcher As New DirectorySearcher(enTry, filter)
        Dim resEnt As SearchResult
        searcher.PropertiesToLoad.Add("userPrincipalName")

        For Each resEnt In searcher.FindAll()
            If resEnt.GetDirectoryEntry.Properties("UserPrincipalName").Count > 0 Then
                TextBoxUPN.Text = resEnt.GetDirectoryEntry.Properties("UserPrincipalName").Value.ToString
            End If
        Next
=====================================================
If I change "userPrincipalName" to "mail" or "name" it returns the information in the textbox.  But either "userPrincipalName" or "sAMAccountName" will generate the datatype error...
I am going to assign 150 points to grave, because he pointed me in a direction.  However, it was an outside contributor who actually provided the following code WHICH WORKED.

        Dim dirEntry As DirectoryEntry
        Dim ldapPath = "LDAP://DC=xxx,DC=yyy,DC=zzz"
        Dim enTry As New DirectoryEntry(ldapPath, "", "")

        Dim filter As String
        filter = String.Format("(&(objectCategory=user)(givenName={0})(sn={1}))", TextBoxFirstName.Text.Trim, TextBoxLastName.Text.Trim)

*************  New code  *********************
        Dim searcher As DirectorySearcher = New DirectorySearcher
        searcher.Filter = filter
        searcher.SearchScope = SearchScope.Subtree
        Dim resEnt As SearchResult
********************************************

        searcher.PropertiesToLoad.Add("userPrincipalName")

        For Each resEnt In searcher.FindAll()
            If resEnt.GetDirectoryEntry.Properties("userPrincipalName").Count > 0 Then
                ListBox1.Items.Add(resEnt.GetDirectoryEntry.Properties("userPrincipalName").Value.ToString)
            End If
        Next


Thanks to all who did contribute..