Iterating domain names

Discussion for developers using MailEnable.
Post Reply
micheld
Posts: 7
Joined: Mon Feb 08, 2010 1:56 pm

Iterating domain names

Post by micheld »

Does anyone know of a way to iterate on the domain names a postoffice has? In pseudo-code, what I'd like to achieve is this:

foreach (Domain d in aParticularPostoffice) {...}

I don't need to edit them, only to get their names so I can have a list of domain aliases per postoffice.

FindFirstDomain and FindNextDomain look like they could do the job, but I haven't been able to use them in any meaningful way, examples are non-existent on the net and documentation is scarce (no description of what they're supposed to do or samples).

Any hints?

Michel

MailEnable
Site Admin
Posts: 4441
Joined: Tue Jun 25, 2002 3:03 am
Location: Melbourne, Victoria Australia

Re: Iterating domain names

Post by MailEnable »

You are correct, the API guide does not provide an example. The API guide is currently under review and will be updated on the web site when it is complete.

In the interim, the following functions should provide an indication as to the workings of the Domain Class of the Management Assembly.

Code: Select all

        Private Sub AddDataTableColumns(ByRef oTable As DataTable)
            oTable.Columns.Add("DomainName", GetType(String))
            oTable.Columns.Add("Status", GetType(Long))
            oTable.Columns.Add("RedirectionStatus", GetType(Long))
            oTable.Columns.Add("RedirectionHosts", GetType(String))
        End Sub

        Public Function GetFilteredListDataView(ByVal Postoffice As String, ByVal Filter As String) As System.Data.DataView
            Dim bShowItem As Boolean
            Dim oDV As DataView
            Dim oDT As DataTable
            Dim oRow As System.Data.DataRow

            Me.AccountName = Postoffice
            Me.DomainName = ""
            Me.Status = -1
            Me.DomainRedirectionHosts = ""
            Me.DomainRedirectionStatus = -1

            If FindFirstDomain() = 1 Then
                oDT = New DataTable("Lists")
                AddDataTableColumns(oDT)

                Do
                    bShowItem = False

                    If Len(Filter) > 0 Then
                        If InStr(1, Me.DomainName, Filter, CompareMethod.Text) > 0 Then
                            bShowItem = True
                        End If

                        If InStr(1, Me.DomainRedirectionHosts, Filter, CompareMethod.Text) > 0 Then
                            bShowItem = True
                        End If
                    Else
                        bShowItem = True
                    End If

                    If bShowItem Then
                        oRow = oDT.NewRow

                        ' Domain Name
                        oRow("DomainName") = Me.DomainName

                        ' Status
                        oRow("Status") = Me.Status

                        ' Redirection Status
                        oRow("RedirectionStatus") = Me.DomainRedirectionStatus

                        ' Redirection Hosts
                        oRow("RedirectionHosts") = Me.DomainRedirectionHosts

                        oDT.Rows.Add(oRow)
                    End If

                    Me.AccountName = Postoffice
                    Me.DomainName = ""
                    Me.Status = -1
                    Me.DomainRedirectionHosts = ""
                    Me.DomainRedirectionStatus = -1

                Loop While (FindNextDomain() = 1)

                If oDT.Rows.Count > 0 Then
                    oDV = New DataView(oDT)
                End If

            End If

            Return oDV
        End Function
Regards, Andrew

micheld
Posts: 7
Joined: Mon Feb 08, 2010 1:56 pm

Re: Iterating domain names

Post by micheld »

MailEnable wrote:You are correct, the API guide does not provide an example. The API guide is currently under review and will be updated on the web site when it is complete.

In the interim, the following functions should provide an indication as to the workings of the Domain Class of the Management Assembly.
...
Thanks Andrew, that's just what I was looking for. In case it helps someone, here's a simple iterator to retrieve all the domain names associated with a postoffice as a string array, in c# (no error checking for brevity). Returns an empty array when the postoffice has no associated domains. Obviously requires the ME Management assembly and a "using MailEnable.Administration;" directive.

Code: Select all

public string[] GetDomainNames(string postOffice)
{
	Domain domain = new Domain();
	List<string> list = new List<string>();
	ResetDomain(domain, postOffice);
	if (domain.FindFirstDomain() == 1)
	{
		do
		{
			list.Add(domain.DomainName);
			ResetDomain(domain, postOffice);
		} while (domain.FindNextDomain() == 1);
	}
	return list.ToArray();
}

private void ResetDomain(Domain domain, string accountName)
{
	domain.AccountName = accountName;
	domain.DomainName = domain.DomainRedirectionHosts = "";
	domain.Status = domain.DomainRedirectionStatus = -1;
}
Michel

Post Reply