Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

Returning Multiple Values from AD Query Function

I have a visual basic application which connects to AD, uses the current logged on user's samAccountName to search for the account, and then pulls the extensionAttribute3-9 form the account and displays 1 as text, 3 as comboboxes and 3 as textboxes. However, although I can perform the AD query successfully, I can't get the values to extensionAttribute3-9 to return. Probably because a function can only return 1 result! So how can I get multiple results to be returned? Maybe load the query results into an array, and then return the array? Or some other better method?

My form code to call the function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            Dim currentADUser As System.DirectoryServices.AccountManagement.UserPrincipal
            currentADUser = System.DirectoryServices.AccountManagement.UserPrincipal.Current
            Dim DisplayName As String = currentADUser.GivenName & " " & currentADUser.Surname
            Username.Text = DisplayName
            extension3.Text = GetUserProperties()
            extension4TextBox.Text = GetUserProperties()
            extension5ComboBox.SelectedText = GetUserProperties()
        Catch ex As Exception
            MsgBox("No Connection to domain." & Environment.NewLine & "Please connect to corporate network & try again.", MsgBoxStyle.Critical, "Network Error #1")
            Application.Exit()
        End Try
    End Sub

Open in new window


My GetUserProperties() function code

 Private Function GetUserProperties()
        Dim ADName As String = GetLogonName()
        Dim bSuccess As Boolean = False
        Dim dirEntry As DirectoryEntry = GetDirectoryEntry()
        Dim dirSearcher As DirectorySearcher = New DirectorySearcher(dirEntry)
        Dim extension3 As Integer
        Dim extension4 As String
        dirSearcher.Filter = ("(samAccountName=" & ADName & ")")
        dirSearcher.PropertiesToLoad.Add("extensionAttribute3")
        dirSearcher.PropertiesToLoad.Add("extensionAttribute4")
        dirSearcher.PropertiesToLoad.Add("extensionAttribute5")
        dirSearcher.PropertiesToLoad.Add("extensionAttribute6")
        dirSearcher.PropertiesToLoad.Add("extensionAttribute7")
        dirSearcher.PropertiesToLoad.Add("extensionAttribute8")
        dirSearcher.PropertiesToLoad.Add("extensionAttribute9")
        dirSearcher.SearchScope = SearchScope.Subtree
        Try
            Dim dirResult As SearchResult = dirSearcher.FindOne()
            bSuccess = Not (dirResult Is Nothing)
            If dirResult Is Nothing OrElse dirResult.GetDirectoryEntry.Properties("extensionAttribute3").Value Is Nothing Then
                Return "<Not Set>"
            Else
                extension3 = (dirResult.Properties("extensionAttribute3")(0).ToString())
                extension4 = (dirResult.Properties("extensionAttribute4")(0).ToString())
                extension5 = (dirResult.Properties("extensionAttribute5")(0).ToString())
            End If
            Return extension3
        Catch ex As Exception
            bSuccess = False
            MsgBox("No Connection to the domain." & Environment.NewLine & "Please connect to corporate network & try again.", MsgBoxStyle.Critical, "Network Error #2")
            Application.Exit()
        End Try
        Return False
    End Function

Open in new window

0
bjblackmore
Asked:
bjblackmore
  • 3
  • 3
2 Solutions
 
p_sieCommented:
There are multiple options.
1. create 3 public variables in a module and assign the values to that variables in the function. Then you can use the variables in the form_load sub
2. create a structure or class with the 3 variables in it and let the function return a value of this structure or class.
0
 
ChloesDadCommented:
Another way would be to overload the function, have one return string, one a combo box and one a textbox

Private overloads Function GetUserProperty(attributename as string) as String

return somestring
end function

Private overloads Function GetUserProperty(attributename as string) as combobox

return somecombobox
end function

Private overloads Function GetUserProperty(attributename as string) as textbox

return sometextbox
end function

Open in new window


Then in the calling sub have 1 call to return a string, 3 to return a combobox and 3 to return a textbox. The code in each subroutine will be the same apart from this line

sometext= dirResult.Properties(attributename)(0).ToString

or

SomeCombobox= trycast(dirResult.Properties(attributename)(0),combobox)

or

Sometextbox = trycast(dirResult.Properties(attributename)(0),textbox)
0
 
bjblackmoreAuthor Commented:
Do you have any example code I could use/modify? Or easy to follow tutorials? My visual basic skills are very basic. The code above is stuff I've managed get get from other projects/sites, and modify for use.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
bjblackmoreAuthor Commented:
Thanks for the example code ChloesDad. I'll give it a try!
0
 
p_sieCommented:
Hi bjblackmore,

I tried to implement my suggestion in some code:

 Private Structure ADProperties
        Public Three As String
        Public Four As String
        Public Five As String
    End Structure

    Private Function GetUserProperties() As ADProperties

        '.... other code
        GetUserProperties.Three = "hello"
        GetUserProperties.Four = "number 4"
        GetUserProperties.Five = "Five"
        '.... more other code

    End Function

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            Dim currentADUser As System.DirectoryServices.AccountManagement.UserPrincipal
            currentADUser = System.DirectoryServices.AccountManagement.UserPrincipal.Current
            Dim DisplayName As String = currentADUser.GivenName & " " & currentADUser.Surname
            Username.Text = DisplayName

            Dim ADProp As ADProperties = GetUserProperties()
            extension3.Text = ADProp.Three
            extension4TextBox.Text = ADProp.Four
            extension5ComboBox.SelectedText = ADProp.Five
        Catch ex As Exception
            MsgBox("No Connection to domain." & Environment.NewLine & "Please connect to corporate network & try again.", MsgBoxStyle.Critical, "Network Error #1")
            Application.Exit()
        End Try
    End Sub

Open in new window


You declare a structure ADProperties
Let the returnvalue of the function be of this structure
The declare a variable as this structure in form_load en set the value to the function GetUserProperties.
The you can set the values to the textboxes.

This is of course extendable to more variables in the structure.
0
 
bjblackmoreAuthor Commented:
Code worked. Was able to modify for my app use, and extend to a further 9 user attributes. Also the code sample helped me to easily understand how/why this worked, so hopefully I can use this again in the future!
0
 
p_sieCommented:
Glad to hear it help you!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now