Dave McConnell
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() .Propertie s("givenNa me").Value () it is no problem. However, if I try TextBox1.Text = resEnt.GetDirectoryEntry() .Propertie s("userPri ncipalName ").Value() then I receive an error about "The directory datatype cannot be converted to/from a native DS datatype".
ASKER
I should have included the code:
Dim sourcename As String
Dim dirEntry As DirectoryEntry
Dim ldapPath = "LDAP://DC=xxx,DC=ds,DC=xx x"
Dim enTry As New DirectoryEntry(ldapPath, "", "")
Dim filter As String = String.Format("(&(objectCa tegory=use r)(givenNa me={0})(sn ={1}))", TextBoxFirstName.Text, TextBoxLastName.Text)
Dim searcher As New DirectorySearcher(enTry, filter)
Dim resEnt As SearchResult
' Console.WriteLine("Search Results = " & searcher.FindAll().ToStrin g())
For Each resEnt In searcher.FindAll()
searcher.PropertiesToLoad. Add("userP rincipalNa me")
Next
TextBoxUPN.Text = resEnt.GetDirectoryEntry() .Propertie s("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
Dim sourcename As String
Dim dirEntry As DirectoryEntry
Dim ldapPath = "LDAP://DC=xxx,DC=ds,DC=xx
Dim enTry As New DirectoryEntry(ldapPath, "", "")
Dim filter As String = String.Format("(&(objectCa
Dim searcher As New DirectorySearcher(enTry, filter)
Dim resEnt As SearchResult
' Console.WriteLine("Search Results = " & searcher.FindAll().ToStrin
For Each resEnt In searcher.FindAll()
searcher.PropertiesToLoad.
Next
TextBoxUPN.Text = resEnt.GetDirectoryEntry()
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("UserPri ncipalName ").Count > 0 then
SomeText = resEnt.Properties("UserPri ncipalName ").Value.T oString
end if
Inside the For Each loop, you should retrieve the property like this:
if resEnt.Properties("UserPri
SomeText = resEnt.Properties("UserPri
end if
ASKER
========================== ========== ========== ========== ========== =====
searcher.PropertiesToLoad. Add("userP rincipalNa me")
For Each resEnt In searcher.FindAll()
If resEnt.Properties("userPri ncipalName ").Count > 0 Then
TextBoxUPN.Text = resEnt.GetDirectoryEntry() .Propertie s("userPri ncipalName ").Value() .ToString
End If
Next
========================== ========== ========== ========== ========== =====
This gives me the same error about "datatype cannot be converted".
========================== ========== ========== ========== ========== =====
searcher.PropertiesToLoad. Add("userP rincipalNa me")
For Each resEnt In searcher.FindAll()
If resEnt.Properties("userPri ncipalName ").Count > 0 Then
TextBoxUPN.Text = resEnt.Properties("userPri ncipalName ").Value.T oString
End If
Next
========================== ========== ========== ========== ========== =====
When I try to compile I get: "MainForm.vb(185): 'Value' is not a member of 'System.DirectoryServices. ResultProp ertyValueC ollection' ."
========================== ========== ========== ========== ========== =====
searcher.PropertiesToLoad. Add("userP rincipalNa me")
For Each resEnt In searcher.FindAll()
If resEnt.Properties("userPri ncipalName ").Count > 0 Then
TextBoxUPN.Text = resEnt.Properties("userPri ncipalName ").ToStrin g
End If
Next
========================== ========== ========== ========== ========== =====
If I remove ".value" it will compile and run, but I get this in the textbox: "System.DirectoryServices. ResultProp ertyValueC ollection" .
Still need some help here...
searcher.PropertiesToLoad.
For Each resEnt In searcher.FindAll()
If resEnt.Properties("userPri
TextBoxUPN.Text = resEnt.GetDirectoryEntry()
End If
Next
==========================
This gives me the same error about "datatype cannot be converted".
==========================
searcher.PropertiesToLoad.
For Each resEnt In searcher.FindAll()
If resEnt.Properties("userPri
TextBoxUPN.Text = resEnt.Properties("userPri
End If
Next
==========================
When I try to compile I get: "MainForm.vb(185): 'Value' is not a member of 'System.DirectoryServices.
==========================
searcher.PropertiesToLoad.
For Each resEnt In searcher.FindAll()
If resEnt.Properties("userPri
TextBoxUPN.Text = resEnt.Properties("userPri
End If
Next
==========================
If I remove ".value" it will compile and run, but I get this in the textbox: "System.DirectoryServices.
Still need some help here...
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
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=yy y,DC=gov"
Dim enTry As New DirectoryEntry(ldapPath, "", "")
Dim filter As String = String.Format("(&(objectCa tegory=use r)(givenNa me={0})(sn ={1}))", TextBoxFirstName.Text, TextBoxLastName.Text)
Dim searcher As New DirectorySearcher(enTry, filter)
Dim resEnt As SearchResult
searcher.PropertiesToLoad. Add("userP rincipalNa me")
For Each resEnt In searcher.FindAll()
If resEnt.GetDirectoryEntry.P roperties( "UserPrinc ipalName") .Count > 0 Then
TextBoxUPN.Text = resEnt.GetDirectoryEntry.P roperties( "UserPrinc ipalName") .Value.ToS tring
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...
here is the code again:
==========================
Dim sourcename As String
Dim dirEntry As DirectoryEntry
Dim ldapPath = "LDAP://DC=xxx,DC=ds,DC=yy
Dim enTry As New DirectoryEntry(ldapPath, "", "")
Dim filter As String = String.Format("(&(objectCa
Dim searcher As New DirectorySearcher(enTry, filter)
Dim resEnt As SearchResult
searcher.PropertiesToLoad.
For Each resEnt In searcher.FindAll()
If resEnt.GetDirectoryEntry.P
TextBoxUPN.Text = resEnt.GetDirectoryEntry.P
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...
ASKER
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=z zz"
Dim enTry As New DirectoryEntry(ldapPath, "", "")
Dim filter As String
filter = String.Format("(&(objectCa tegory=use r)(givenNa me={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("userP rincipalNa me")
For Each resEnt In searcher.FindAll()
If resEnt.GetDirectoryEntry.P roperties( "userPrinc ipalName") .Count > 0 Then
ListBox1.Items.Add(resEnt. GetDirecto ryEntry.Pr operties(" userPrinci palName"). Value.ToSt ring)
End If
Next
Thanks to all who did contribute..
Dim dirEntry As DirectoryEntry
Dim ldapPath = "LDAP://DC=xxx,DC=yyy,DC=z
Dim enTry As New DirectoryEntry(ldapPath, "", "")
Dim filter As String
filter = String.Format("(&(objectCa
************* New code *********************
Dim searcher As DirectorySearcher = New DirectorySearcher
searcher.Filter = filter
searcher.SearchScope = SearchScope.Subtree
Dim resEnt As SearchResult
**************************
searcher.PropertiesToLoad.
For Each resEnt In searcher.FindAll()
If resEnt.GetDirectoryEntry.P
ListBox1.Items.Add(resEnt.
End If
Next
Thanks to all who did contribute..
So, most likely, there's nothing wrong with your code (other than it doesn't check to see if the userPrincipalName attribute exists)