You are viewing limited content. For full access, please sign in.

Question

Question

RA LF Folder Picker (VB.NET)

asked on February 6, 2014

 I have shared a folder picker in the past based on LFSO.  Here I will share an RA based folder browser/picker.

In your VB project, add an new Dialog item (I named mine PFTBrowser).  Add a Panel (its name does not matter) to the Dialog form and set it to cover most of the form.  Now place a TreeView object anywhere inside the panel and name it tvFolderTree.

 

Here is the code for the PFTBrowse Dialog form:

'***********************************************************
'***********************************************************
'**               PFTBrowser by Bert Warren               **
'**         Copyright © Paper Free Technology 2008        **
'**               Laserfiche Folder Browser               **
'***********************************************************
'***********************************************************

'    PFTBrowser
' This Dialog Window is made up of a panel and treeview for
' displaying the LF Folder structure, and a TableLayoutPanel
' with the OK & Cancel buttons.
' To use this item, instantiate using the keyword New and
' provide valid LF server, repository, windows authentication,
' user name, and password.  This will cause it to create the
' form and log into the repository.  Then when the form is
' closed, it will log out.
'   Usage
'       Dim myBrowser As New PFTBrowser(sLFServerName, sLFRepositoryName, bLFWinAuth, sLFUserName, sLFPassword)
'       'Open/Show new PFTBrowser Dialog
'       myBrowser.ShowDialog()
'       ' Check dialog result
'       If myBrowser.DialogResult = Windows.Forms.DialogResult.OK Then
'       'If OK was clicked, set _Path = to selected folder path
'       'and/or _EntryID = to selected folder Entry ID
'           Dim _Path As String = myBrowser.Path
'           Dim _EntryID As Integer = myBrowser.EntryId
'       End If
'       ' Cleanup
'       myBrowser = Nothing

Imports System.Windows.Forms
Imports Laserfiche.RepositoryAccess

Public Class PFTBrowser

    Private myLFSession As Session = Nothing

    '*********************************
    '*********************************
#Region "Public Properties for Browser"
    Private _EntryId As Integer
    Public ReadOnly Property EntryId() As Integer
        Get
            Return _EntryId
        End Get
    End Property

    Private _Path As String
    Public ReadOnly Property Path() As String
        Get
            Return _Path
        End Get
    End Property

    Private _sServerName As String
    Public WriteOnly Property LFServerName() As String
        Set(value As String)
            _sServerName = value
        End Set
    End Property

    Private _sRepositoryName As String
    Public WriteOnly Property LFRepositoryName() As String
        Set(value As String)
            _sRepositoryName = value
        End Set
    End Property

    Private _sUserName As String
    Public WriteOnly Property LFUserName() As String
        Set(value As String)
            _sUserName = value
        End Set
    End Property

    Private _sPassword As String
    Public WriteOnly Property LFPassword() As String
        Set(value As String)
            _sPassword = value
        End Set
    End Property

    Private _bWinAuth As Boolean
    Public WriteOnly Property bLFWinAuth() As Boolean
        Set(value As Boolean)
            _bWinAuth = value
        End Set
    End Property

#End Region
    '*********************************
    '*********************************
#Region "Private LF Log In/Out"

    Private Sub LFLogout()
        ' Only process if LFDatabase object is not nothing 
        If (myLFSession IsNot Nothing) Then
            Try
                ' Log Out 
                myLFSession.LogOut()
                myLFSession.Discard()
                ' Log Errors Here 

            Catch ex As Exception
            End Try
            ' Ensure LFDatabse object is nothing 
            myLFSession = Nothing
        End If
    End Sub

    Private Sub LFLogin(sServer As String, sRepo As String, bWinAuth As Boolean, Optional sUser As String = "admin", Optional sPW As String = "")
        ' Only process if LFDatabase object is nothing 
        If myLFSession Is Nothing Then
            Try
                ' Create LFServer object for server 
                Dim lfserv As New Server(sServer)
                ' Set LFRepository object 
                Dim LFRepo As New RepositoryRegistration(lfserv, sRepo)
                myLFSession = New Session()
                myLFSession.ApplicationName = Application.ProductName
                ' Process Authentication 
                If bWinAuth Then
                    ' Use blank User and Password for WinAuth 
                    myLFSession.LogIn(LFRepo)
                Else
                    'Do not use empty user name for LF Auth (if that is what was passed) 
                    If String.IsNullOrEmpty(sUser) Then
                        ' Try using admin with blank password 
                        sUser = "admin"
                        sPW = ""
                    End If
                    myLFSession.LogIn(sUser, sPW, LFRepo)
                End If
            Catch ex As Exception
                ' Log Errors Here 
                MessageBox.Show(ex.Message)
                ' Ensure Session object is nothing 
                myLFSession = Nothing
            End Try
        End If
    End Sub

#End Region
    '*********************************
    '*********************************
#Region "Private Subs & Functions"

    Private Sub InitTV()
        ' Create new Node
        Dim RootTreeNode As New TreeNode
        ' Set Node Text = Repository name
        RootTreeNode.Text = myLFSession.Repository.Name
        ' Make new Node expandable
        RootTreeNode.Nodes.Add("")
        ' Add new Node to TreeView
        tvFolderTree.Nodes.Add(RootTreeNode)
        ' Expand new Node
        tvFolderTree.Nodes.Item(0).Expand()
    End Sub

    Private Sub tvFolderTree_AfterExpand(sender As System.Object, e As System.Windows.Forms.TreeViewEventArgs) Handles tvFolderTree.AfterExpand
        ' Clear any existing child nodes
        e.Node.Nodes.Clear()
        ' Get full path from node
        Dim sFullPath As String = e.Node.FullPath
        ' Remove repository name from path for LF use
        If sFullPath.Contains("\") Then
            sFullPath = sFullPath.Replace(sFullPath.Substring(0, sFullPath.IndexOf("\")), "")
        Else
            sFullPath = "\"
        End If
        Try
            'Create FolderInfo object fron Node path
            Dim LFFold As FolderInfo = Folder.GetFolderInfo(sFullPath, myLFSession)
            ' configure which columns to retrieve
            Dim entrySetting As New EntryListingSettings()
            ' Only get folders
            entrySetting.EntryFilter = EntryTypeFilter.Folders
            ' Include Entry Type
            entrySetting.AddColumn(SystemColumn.EntryType)
            ' Include Entry Name
            entrySetting.AddColumn(SystemColumn.DisplayName)
            ' get the contents of the lf folder
            Using listing As FolderListing = LFFold.OpenFolderListing(entrySetting, 1000)
                ' the listing is 1-based, 
                Dim rowCount As Integer = listing.RowsCount
                For i As Integer = 1 To rowCount
                    ' Process only folders
                    ' Listing should never get anything other than folder, but will check anyway
                    If listing.GetDatumAsString(i, SystemColumn.EntryType).ToLower = "folder" Then
                        ' Create new Node
                        Dim NewTreeNode As New TreeNode
                        ' Set Node Text = folder name
                        NewTreeNode.Text = listing.GetDatumAsString(i, SystemColumn.DisplayName)
                        ' Make new Node expandable
                        NewTreeNode.Nodes.Add("")
                        ' Add new Node to TreeView
                        e.Node.Nodes.Add(NewTreeNode)
                    End If
                Next
            End Using
            ' Cleanup
            LFFold.Dispose()
            LFFold = Nothing
        Catch ex As Exception

        End Try
    End Sub

    Private Sub tvFolderTree_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvFolderTree.AfterSelect
        ' Get full path from node
        Dim sFullPath As String = e.Node.FullPath
        ' Remove repository name from path for LF use
        If sFullPath.Contains("\") Then
            sFullPath = sFullPath.Replace(sFullPath.Substring(0, sFullPath.IndexOf("\")), "")
        Else
            sFullPath = "\"
        End If
        Try
            'Create FolderInfo object from Node path
            Dim LFFold As FolderInfo = Folder.GetFolderInfo(sFullPath, myLFSession)
            ' Get Entry ID of selected Node
            _EntryId = LFFold.Id
            ' Get full path of folder
            _Path = LFFold.Path
            ' Cleanup
            LFFold.Dispose()
            LFFold = Nothing
        Catch ex As Exception

        End Try
    End Sub

#End Region
    '*********************************
    '*********************************
#Region "Button Actions for Browser"

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

#End Region
    '*********************************
    '*********************************
#Region "Form Actions for Browser"

    Public Sub New(ByVal LFServerName As String, ByVal LFRepositoryName As String, ByVal UseWindowsAuthentication As Boolean, ByVal LFUserName As String, ByVal LFPassword As String)
        MyBase.New()
        InitializeComponent()
        LFLogin(LFServerName, LFRepositoryName, UseWindowsAuthentication, LFUserName, LFPassword)
    End Sub

    Private Sub PFTBrowser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Make TreeView fill panel1
        Me.tvFolderTree.Dock = DockStyle.Fill
        ' Initialize TreeView
        InitTV()
    End Sub

    Private Sub PFTBrowser_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        ' Cleanup
        LFLogout()
    End Sub

#End Region
    '*********************************
    '*********************************
End Class

 

8 0

Replies

replied on March 16, 2015

What no comments?  Bert, you continue your awesomeness with a whole tutorial on RA and how to navigate it's many objects and nuances. Three thumbs up!

0 0
replied on September 24, 2015

Hi Bert,

You are great! This is just what I needed, I just translated this to C# and it's working, now I just need to find out how to create a document based on the folder picked (on the tree view). 

 

Thank you so much! 

0 0
replied on September 25, 2015 Show version history

See Using RA to create a document, assign a template and populate fields for code sample of how to create a document.

In the code line to get the parent folder object, pass the path or Folder ID that is returned from the picker.

 

EDIT:

You can also look at RA_Demo_Import_VB 2010 project to see how I create LF Folders and Documents.

3 0
replied on September 25, 2015

Thank you! Just what I needed 

0 0
replied on September 22, 2018

Does anyone have this converted to C# by any chance?  <---Sheepish Smile

0 0
replied on December 19, 2019 Show version history

Hello Bert,

 

I have a question.  Maybe I missed it in your code, but I need to write a .net import program using RA.  I am able to create the document in laserfiche and update the template fields and I get the expected results. 

 

 

Dim RARepoReg As RepositoryRegistration = New RepositoryRegistration(vServerName,vLFDB)

Dim RASess As New Session

RASess.LogIn(vUserName,vUserPassword,RARepoReg)

 

 

Dim docImporter As DocumentImporter = New DocumentImporter

 

Dim entryID As Integer = Document.Create(vLFFolder & vCaseNumber & " - " & vDYear & "-" & vDMonth & "-" & vDDay & " - " & vDocName,"CRT_VOLUME_NEW1", EntryNameOption.AutoRename, RASess)

 

docImporter.Document = Document.GetDocumentInfo(entryID,RASess)

 

docImporter.ImportEdoc("application/pdf", "G:\Laserfiche\ImportDocs\UnSealed\19-SC-1234 - 2019-10-09 - PETITION.PDF")

 

Dim EI As EntryInfo = Entry.GetEntryInfo(entryID,RASess)

Dim FVC As FieldValueCollection = EI.GetFieldValues()

 

EI.SetTemplate("Civil Court Filing")

FVC.Item ("File Stamp") = vDocDate

FVC.Item ("Document Type") = vDocName

FVC.Item ("Case Number") = vCaseNumber

EI.Lock(LockType.Exclusive)

EI.SetFieldValues(FVC)

EI.Save()

EI.Unlock()

 

docImporter = Nothing

RASess.Close()

RASess = Nothing

 

Return 0

End Function

 

 

End Class

 

What I need to do now is create a folder if it doesn't exist or use it if it does exist.  I have not found any samples of how to do this.  I haven't found too much documention on Laserfiche SDK.

 

Thank you,

Gary Schreader

0 0
replied on December 19, 2019

Hi Gary,

One thing to note - there is pretty extensive SDK documentation, but it's not online at this time. If you install the main SDK installation, it will install a number of help files along with it. 

0 0
replied on December 19, 2019

Hi Justin,

 

I have installed the SDK with the help files and I didn't see anything in the Tutorials with Documentation on adding folders.  I am not as familiar with C# so the line of code I tried gave me an error. 

Folder.Create(vLFFolder,"CRT_VOLUME_NEW1", EntryNameOption.Overwrite, RASess)

I have had better luck looking at code somebody like Bert has written and building off that.

0 0
replied on December 19, 2019 Show version history

There is a VC# code sample of creating folders in a path if they do not exist by Matthew Kelly in the below link

https://answers.laserfiche.com/questions/51440/check-to-see-if-folder-

It is a bit dated, but still a good method.

Below is a slightly different version in VB

    Private Function GetFolder(ByVal sPath As String) As FolderInfo
        ' Ensure we start at the root
        If Not sPath.StartsWith("\") Then
            If sPath.Contains("\") Then
                ' Check if the Path starts with the Repository name
                If sPath.Split("\").First().ToLower() = CurSession.Repository.Name.ToLower() Then
                    ' Remove the repository name from path if it is presant to set the root
                    sPath = sPath.Replace(sPath.Split("\").First(), "")
                Else
                    ' Path does not start with the repository name or a \, so set the root
                    sPath = "\" & sPath
                End If
            Else
                ' Path does not start with a \, so set the root
                sPath = "\" & sPath
            End If
        End If
        ' Try to get folder
        Dim CurFolder As FolderInfo = DirectCast(Entry.TryGetEntryInfo(sPath, CurSession), FolderInfo)
        If CurFolder Is Nothing Then
            ' Folder does not exist, so roll back the path until we get a folder
            Dim toCreate As Stack(Of String) = New Stack(Of String)()
            While (CurFolder Is Nothing)
                ' Push last folder in path onto the stack
                toCreate.Push(sPath.Split("\").Last())
                ' Remove last folder from the path
                sPath = sPath.Substring(0, sPath.LastIndexOf("\"))
                ' Ensure path is not empty
                If (sPath = "") Then
                    ' Set empty path to root
                    sPath = "\"
                End If
                ' Try to get the folder from the new path
                CurFolder = DirectCast(Entry.TryGetEntryInfo(sPath, CurSession), FolderInfo)
            End While
            ' Step through the stack creating needed folders
            Dim parent As FolderInfo = Nothing
            Try
                While (toCreate.Count > 0)
                    ' ensure Parent object is disposed
                    If (parent IsNot Nothing) Then
                        parent.Dispose()
                    End If
                    ' set Parent = to current folder to create next folder in path
                    parent = CurFolder
                    ' reset current folder to create the next folder in the path
                    CurFolder = New FolderInfo(CurSession)
                    ' Get the name of the next folder
                    sPath = toCreate.Pop()
                    ' Create the next folder
                    CurFolder.Create(parent, sPath, EntryNameOption.AutoRename)
                End While
            Catch ex As Exception
                ' Guarantee that folder does not leak if an Exception is thrown.
                If (CurFolder IsNot Nothing) Then
                    CurFolder.Dispose()
                    CurFolder = Nothing
                End If
                Throw
            Finally
                ' Guarantee that parent does not leak.
                If (parent IsNot Nothing) Then
                    parent.Dispose()
                End If
            End Try
        End If
        Return CurFolder
    End Function

 

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.