VB Code to get information from Active Direcoty

C0oli0

Member
Joined
Oct 13, 2010
Messages
8
Programming Experience
Beginner
Hello, I just signed up today, and i apologize if i posted in wrong section.
It's my first time using VB or VSTA, I a have no programming knowledge.

I'm creating an infopath timesheet, and im trying to have it set up to grab the persons name and manager from AD.

I read from a MS site on how to do it, but it was not clear enough for a beginner. Site: Adding User Information from Active Directory Domain Services to InfoPath 2007 Forms

Now, I've taken a screenshot of what i did, but i really need help as i have to have this set up by this week.

I'll try and paste the top and the end.
I received 3 errors on the code:
1:Type 'DirectorySearcher' is not defined.
2:Type 'SeatchResult' is not defined
3:Type 'DirectoryEnTry' is not defined.

The code i had to add was:

Try
' Get the user name of the current user.
Dim userName As String = Me.Application.User.UserName

' Create a DirectorySearcher object using the user name
' as the LDAP search filter. If using a directory other
' than Exchange, use sAMAccountName instead of mailNickname.
Dim searcher As DirectorySearcher = New DirectorySearcher( _
"(mailNickname=" + userName + ")")

' Search for the specified user.
Dim result As SearchResult = searcher.FindOne()

' Make sure the user was found.
If result Is Nothing Then
MessageBox.Show("Error finding user: " + userName)
Else
' Create a DirectoryEntry object to retrieve the collection
' of attributes (properties) for the user.
Dim employee As DirectoryEnTry = result.GetDirectoryEnTry()

' Assign the specified properties to string variables.
Dim FirstName As String = employee.Properties( _
"givenName").Value.ToString()
Dim LastName As String = employee.Properties( _
"sn").Value.ToString()
Dim Mail As String = employee.Properties( _
"mail").Value.ToString()
Dim Location As String = employee.Properties( _
"physicalDeliveryOfficeName").Value.ToString()
Dim Title As String = employee.Properties( _
"title").Value.ToString()
Dim Phone As String = employee.Properties( _
"telephoneNumber").Value.ToString()
Dim Department As String = employee.Properties( _
"department").Value.ToString()

' The manager property returns a distinguished name,
' so get the substring of the common name following "CN=".
Dim ManagerName As String = employee.Properties( _
"manager").Value.ToString()
ManagerName = ManagerName.Substring( _
3, ManagerName.IndexOf(",") - 3)

' Create an XPathNavigator to walk the main data source
' of the form.
Dim xnMyForm As XPathNavigator = Me.CreateNavigator()
Dim ns As XmlNamespaceManager = Me.NamespaceManager

' Set the fields in the form.
xnMyForm.SelectSingleNode("/my:myFields/my:FirstName", ns) _
.SetValue(FirstName)
xnMyForm.SelectSingleNode("/my:myFields/my:LastName", ns) _
.SetValue(LastName)
xnMyForm.SelectSingleNode("/my:myFields/my:Alias", ns) _
.SetValue(userName)
xnMyForm.SelectSingleNode("/my:myFields/my:Email", ns) _
.SetValue(Mail)
xnMyForm.SelectSingleNode("/my:myFields/my:Manager", ns) _
.SetValue(ManagerName)
xnMyForm.SelectSingleNode("/my:myFields/my:Location", ns) _
.SetValue(Location)
xnMyForm.SelectSingleNode("/my:myFields/my:Title", ns) _
.SetValue(Title)
xnMyForm.SelectSingleNode("/my:myFields/my:TelephoneNumber", _
ns).SetValue(Phone)
xnMyForm.SelectSingleNode("/my:myFields/my:Department", ns) _
.SetValue(Department)

' Clean up.
xnMyForm = Nothing
searcher.Dispose()
result = Nothing
employee.Close()
End If

Catch ex As Exception
MessageBox.Show("The following error occurred: " + _
ex.Message.ToString())
Throw
End Try
 

Attachments

  • Beginning of Code.png
    Beginning of Code.png
    15.9 KB · Views: 74
  • End of Code.png
    End of Code.png
    12.2 KB · Views: 74
You need to add a reference to System.DirectoryServices.

Then you need to add an import line at the top of your file

VB.NET:
Imports System.DirectoryServices
 
Hello MattP, Thanks for the reply.

When you say i need to add a reference...what do you mean as this is my first time using VB.

Also, Where would the import line go? At the end of the code? At the end of a certain line? Beginning of where? I know you said at the top of the file, but top of what file? The VB code screen or infopath screen?

Thanks for your help. Im working for a company which uses AD.
 
1. Project --> Add Reference --> .NET tab --> System.DirectoryServices --> Ok

2. At the top of the code behind right under Imports mshtml would be fine.
 
MattP, thanks a bunch as that worked and got rid of the errors.

However, i noticed i missed one part as well, it said:

At the top of the code module below the existing using statements, add the following statement:

"This language is not supported or no code example is available." minus the quotations. Where does it mean for this to go?

Thanks again for the help!
 
Looks like it's a screw up and there's 2 code blocks in that section with C# in one and VB in the other rather than spanning tabs.
 
I'm only using VB so i hope i was only following the VB Instructions. After putting in the code, it gave no errors, but i cant even open up the infopath form, saysthere is an error with the code....sighs programming isnt that easy.
 
After i did everything, it showed no errors. So i saved it all, now i cant even open the form. It gives error it cannot open the form. I have webex if you have time to join and have a look.

This is just extremely important and i have to have it done by next Wednesday.

I've attached a quick ss of the error.
 

Attachments

  • Error when opening.png
    Error when opening.png
    16.8 KB · Views: 47
Hello,

Tried that, but could never get past step 3 as i dont know what exactly it wants for the address.

Tried our own sharepoint site, but wouldnt accept, so no idea how that would work.
 
Hi there,

Just wondering, did you ever get around this?

We have WSS V3.0 and we have the same issue... it seems that the web service needed to pick out users from AD doesn't exisit in WSS, only MOSS.

Hence - if you try step 3 in the above link from MattP it will fail on the web service .aspx address....

It appears that we need to buy a product from QDABRA in order to get this to function!
 
To jaybranch:
No, sadly I never found a solution for this. But yes, Qdabara has the AD function and i guess it is pretty cheap.

Nothing is easy in life!
I have lots of respect for programmers, i have no idea how they do it!
 
Back
Top