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
epartment", 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
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
.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