Infopath retreiving AD properties on Users

R

RJ

In my form, how do I get the form to populate fields from AD when i have the
function set up as get username( ) How do i get it to pull the actual
employee ID, the branch they are located at and the extension which is all
available in AD. Please help for some direction. Thank you.
 
P

Paul D. Fox

Here is some sample code I use to populate some fields in the form...

Public Sub GetUserInformation()

Try

' Get the user name of the current user.

Dim strUserName As String = Me.Application.User.UserName

' Create a DirectorySearcher object using the user name

' as the Active Directory LDAP search filter. If using a directory other

' than Exchange, use sAMAccountName instead of mailNickname.

Dim ADsSearcher As DirectorySearcher = New
DirectorySearcher("(sAMAccountName=" + strUserName + ")")

' Search for the specified user.

Dim ADsSearchResult As SearchResult = ADsSearcher.FindOne()

' Make sure the user was found.

If ADsSearchResult Is Nothing Then

'How do you throw a messageBox in a Browser Enabled InfoPath Form

'MessageBox.Show("Error finding user: " + strUserName + " contact your site
Administrator.")

Else

' Create a DirectoryEntry object to retrieve the collection

' of attributes (properties) for the user.

Dim ADsEmployee As DirectoryEntry = ADsSearchResult.GetDirectoryEntry()

' Assign the specified properties to string variables.

Dim strName As String = ADsEmployee.Properties("cn").Value.ToString()

Dim strMail As String = ADsEmployee.Properties("mail").Value.ToString()

Dim strPhone As String =
ADsEmployee.Properties("telephoneNumber").Value.ToString()

' The manager property returns a distinguished name,

' so get the substring of the common name following "CN=".

'Dim strManagerName As String =
ADsEmployee.Properties("manager").Value.ToString()

'strManagerName = ManagerName.Substring(3, strManagerName.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:MSWORFields/my:InitiatorName",
ns).SetValue(strName)

xnMyForm.SelectSingleNode("/my:MSWORFields/my:InitiatorEmailAddress",
ns).SetValue(strMail)

xnMyForm.SelectSingleNode("/my:MSWORFields/my:InitiatorPhone",
ns).SetValue(strPhone)

' Clean up.

xnMyForm = Nothing

ADsSearcher.Dispose()

ADsSearchResult = Nothing

ADsEmployee.Close()

End If

Catch ex As Exception

'Requires System.Windows.Forms Namespace

'MessageBox.Show("The following error occurred: " + ex.Message.ToString() +
" contact your site Administrator.")

Throw

End Try

End Sub
 
R

RJ

Will this work on web browser form, i know it needs webservice and if that is
the case then the webservice using C# code will retrieve the properties for
me. I am a newbie. thanks for your time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top