Select Page

LibRETS in VB.NET (Summary)

Getting Started

Here are the steps to add libRETS to a VB.NET project:

  1. Download the latest Windows binary build of libRETS, and unzip it to a handy directory
  2. Start a new Project/Solution in Visual Studio .NET
  3. Include a Reference to librets-dotnet.dll (found in the “dotnet” subdirectory where you unzipped the library in step 1)
  4. Right-click on your Project, then choose “Add Existing Item”
  5. Browse to the dotnet directory from Step 3, change the file filter to “Executable Files”, then choose “librets-pinvoke.dll”
  6. In your Solution Explorer window, highlight “librets-pinvoke.dll” and, in the Properties pane, change its “Build Action” to “Content”, and its “Copy to Output Directory” action to “Copy if newer”. This step ensures that the unmanaged DLL is always included in your deployment directory.

Using the Library

Now that you have librets available as a namespace, you have access to all of its classes. libRETS is developed largely in a Command pattern, where each object performs an action whose result is returned as a new type of object.

Logging In & Out

The basic, Hello, World! example:

Dim session As New librets.RetsSession("<serverURL>")
 session.Login("<username>", "<password>")
 Dim response As librets.LoginResponse = session.GetLoginResponse()
 If response IsNot Nothing AndAlso response.GetUserInfo() <> "" Then
     Debug.WriteLine "Hello, World!"
     'TODO: Do stuff.
 End If


Building on the previous example, once logged in you can perform a basic property search:

Dim searchURL As String = response.GetSearchURL()
 Dim searchReq As New librets.SearchRequest("Property", "ATSF", "(ST=ACTV)")
 With searchReq
     .SetSelect("<fields you want>")
 End With
 Dim searchRes As librets.SearchResultSet = session.Search(searchReq)
 Dim searchColumns As librets.StringVector = searchRes.GetColumns()
 Dim fsOut As New IO.FileStream(Date.Now.ToString("yyyyMMdd") & ".txt", _
     IO.FileMode.Create, _
     IO.FileAccess.Write, _
 Dim tw As IO.TextWriter = New IO.StreamWriter(fsOut)
 For Each sCol As String In svCols
     tw.Write(sCol & vbTab)
 While searchRes.HasNext
     For Each sCol As String In svCols
          tw.Write(searchRes.GetString(sCol) & vbTab)
 End While

Debugging & Logging

libRETS relies on a delegate subroutine to pipe its (fairly verbose) trace output. You can hook to this simply by adding a subroutine with the proper signature and attaching it to your session:

Private Sub tracer(ByVal type As librets.RetsHttpLogger.Type, ByVal data As Byte())
     Debug.WriteLine(String.Format("{0}{1}{2}", _
          type.ToString, _
          vbTab, _
 End Sub
 ' Then, after creating your session (as above), add the following:
 Dim session As New librets.RetsSession("<serverURL>")
 session.LoggerDelegate = AddressOf tracer
DLU March 16th, 2016