An Alternative To Using The Generic Array From File Function
Jon Bryan Classic Flow Rules, Code Sample, Gist 0 Comments
While looking to improve on my method of getting exceptions or a long list of mail suffixes into an array, to be checked during code execution, I came across this: https://msdn.microsoft.com/en-us/library/windows/desktop/ms696048(v=vs.85).aspx
This seemed to me to be a really nice solution, just defining all exceptions and suffixes within one file, read it in on code execution, then check for existence or whatever in the code.
So, given the following xml file:
<rules-extension-properties> | |
<mail-suffixes> | |
<values>blah.ac.uk,moo.ac.uk,meow.ac.uk,woof.ac.uk,squawk.ac.uk,hiss.ac.uk,neigh.ac.uk</values> | |
</mail-suffixes> | |
<ignore-email-address-errors> | |
<values>smartypants@woof.ac.uk</values> | |
</ignore-email-address-errors> | |
<ignore-functional-id-owner> | |
<values>random.person@someother.ac.uk</values> | |
</ignore-functional-id-owner> | |
</rules-extension-properties> |
Add the System.Xml Import and declare the variables, so they are global:
Imports Microsoft.MetadirectoryServices | |
Imports System | |
Imports System.directoryservices | |
Imports ActiveDs | |
Imports System.Globalization | |
Imports Microsoft.MetadirectoryServices.Logging | |
Imports System.IO | |
Imports System.Xml | |
Public Class MAExtensionObject_MYADMA | |
Implements IMASynchronization | |
'Date & Logginglevel variables for logging files: | |
Dim dtDateNowHour As Integer = Date.Now.Hour | |
Dim dtDateNowDay As Integer = Date.Now.Day | |
Dim dtDateNowMonth As Integer = Date.Now.Month | |
Dim dtDateNowYear As Integer = Date.Now.Year | |
Dim loggingLevel As Integer = 0 | |
Dim ValidMailSuffixes As String | |
Dim IgnoreFunctionalIDOwner As String | |
Dim IgnoreEmailAddressErrors As String |
Add the code to read the xml file into the Initialize Sub:
Public Sub Initialize() Implements IMASynchronization.Initialize | |
Try | |
Dim config As XmlDocument = New XmlDocument() | |
config.Load("C:\FIMControl\rules-config.xml") | |
' | |
Dim suffixesnode As XmlNode = config.SelectSingleNode("rules-extension-properties/mail-suffixes") | |
Dim suffixvaluenode As XmlNode = suffixesnode.SelectSingleNode("values") | |
Dim suffixesvalue As String = suffixvaluenode.InnerText | |
ValidMailSuffixes = suffixvaluenode.InnerText | |
' | |
Dim ieaenode As XmlNode = config.SelectSingleNode("rules-extension-properties/ignore-email-address-errors") | |
Dim ieeavaluenode As XmlNode = ieaenode.SelectSingleNode("values") | |
Dim ieaevalue As String = ieeavaluenode.InnerText | |
IgnoreEmailAddressErrors = ieeavaluenode.InnerText | |
' | |
Dim ifionode As XmlNode = config.SelectSingleNode("rules-extension-properties/ignore-functional-id-owner") | |
Dim ifiovaluenode As XmlNode = ifionode.SelectSingleNode("values") | |
Dim ifiovalue As String = ifionode.InnerText | |
IgnoreFunctionalIDOwner = ifiovaluenode.InnerText | |
Catch nre As NullReferenceException | |
'If a tag does not exist in the xml, then the stopped-extension-dll error will be thrown. | |
Throw nre | |
Catch e As Exception | |
Throw e | |
End Try | |
End Sub |
Then, when you wish to look for those values within those variables – just like in the last post:
Case "emailAddressPresent-ADMA-Import" | |
'AD attributes required: mail and msExchHomeServerName | |
' Default setting = False | |
mventry("emailAddressPresent").Value = "False" | |
If csentry("mail").IsPresent And csentry("msExchHomeServerName").IsPresent Then | |
Dim suffix() As String = Split((csentry("mail").Value), "@") | |
If ValidMailSuffixes.Contains(suffix(1).ToLower) Then | |
mventry("emailAddressPresent").Value = "True" | |
Else | |
'If a suffix from the above is not found - raise an error, so that the suffix can be added to the text file or simply sorted out - where a mistake was made. | |
Throw New Exception("Invalid email suffix found: " & suffix(1)) | |
End If | |
ElseIf csentry("mail").IsPresent And csentry("mailNickName").IsPresent Then | |
'This person is a mail enabled user, maybe with an island site email address or just something else, so we want them to be able to be added to distribution lists.... | |
mventry("emailAddressPresent").Value = "True" | |
End If |