Search code examples
vb.netcollectionsblueprism

Using For Each to assign collection to collection data item


Working in Blue Prisms vb.net environment and currently building out an LDAP VBO utility. I have the below code, it will return the list of users in an AD Group i.e. samAccountName, givenname & surname.

My namespace imports used are:

System
System.Drawing, System.Data
System.DirectoryServices
System.Collections.Generic

My issue is with trying to loop through the resultset and store each row of data returned in my final collection data item, colResults.

I know I'm working with a collection and I'm aware that what I'm doing in my For Each loop is not correct, i.e. compile error:

"String cannot be converted to System.Data.Datatable"

But I'm unable to resolve.

I've attempted to rewrite and use an index but syntactically I can't use .Count with my Properties.SearchResultCollection.

Dim de As New DirectoryServices.DirectoryEntry("LDAP://" + Path)
Dim ds As New DirectoryServices.DirectorySearcher(de)
Dim srCol As DirectoryServices.SearchResultCollection
Dim sr As DirectoryServices.SearchResult

'Try and assume success
Success = True

Try

    ds.Filter = "(&(objectCategory=user)(memberOf=" + GroupDN + "))"
    ds.PropertiesToLoad.Add("givenname")
    ds.PropertiesToLoad.Add("sn")
    ds.PropertiesToLoad.Add("samAccountName")

    srCol = ds.FindAll()

    For Each sr In srCol
        If Not sr.GetDirectoryEntry.Properties("name").Value Is Nothing Then
            colResults = (sr.GetDirectoryEntry.Properties("givenname").Value.ToString())
            colResults = (sr.GetDirectoryEntry.Properties("sn").Value.ToString())
            colResults = (sr.GetDirectoryEntry.Properties("samAccountName").Value.ToString)
        End If
    Next
Catch e As System.Exception
    Success = False
End Try

Solution

  • When returning a DataTable from a code stage in Blue Prism, the code stage doesn't know what the columns of the output parameter are going to be – you'd normally assign a new DataTable to it within the code, define the columns, then fill the rows.

    So, you should initialize the colResults DataTable at the beginning of your code stage:

    colResults = new DataTable
    ' make sure the output parameter has these columns defined (or none at all)
    colResults.Columns.Add("givenname")
    colResults.Columns.Add("sn")
    colResults.Columns.Add("samAccountName")
    

    And keep creating DataRows and adding them to the table from within the loop:

    For ...
        If ...
            Dim newRow as DataRow = colResults.NewRow
            newRow("givenname") = ...
            newRow("sn") = ...
            newRow("samAccountName") = ...
            colResults.Rows.Add(newRow)
        End If
    Next
    

    DataRow classes' documentation shows a well-detailed example as well.