Community
Vault Customization
Share your knowledge, ask questions, and explore popular Vault API, Data Standard, and VBA topics related to programming, creating add-ins, or working with the Vault API.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Acquire specific file from specific folder

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
rusmwb
2600 Views, 3 Replies

Acquire specific file from specific folder

I'm having trouble getting a file from folder. I'm using some code from an example I found. But tried to modify it to look in a specific folder.

I can't seem to get the folder id.

 

Sub would be called something like this: DownloadFile("C:/VaultWork/_Designs/Folder1/Filename.ipt")

 

it returns this error message:

A first chance exception of type 'System.Web.Services.Protocols.SoapException' occurred in Autodesk.Connectivity.WebServices.dll

 

 

Private Sub DownloadFile(filename As String)
        Dim results As VDF.Vault.Results.LogInResult = VDF.Vault.Library.ConnectionManager.LogIn("rusgrveng1", _
                                                                                                 "gveng", _
                                                                                                 "username", _
                                                                                                 "password", _
                                                                                                 VDF.Vault.Currency.Connections.AuthenticationFlags.Standard, Nothing)
        If Not results.Success Then
            Return 'exit sub
        End If

        Dim connection As VDF.Vault.Currency.Connections.Connection = Nothing
        connection = results.Connection

        Try
            Dim oFileIteration As VDF.Vault.Currency.Entities.FileIteration = Nothing

            Try
                'Get the file iteration
                oFileIteration = GetFileIteration(filename, connection)
            Catch ex As Exception
                Debug.WriteLine("Could not get file iteration")
            End Try

            If oFileIteration IsNot Nothing Then
                'get settings
                Dim oSettings As VDF.Vault.Settings.AcquireFilesSettings = New VDF.Vault.Settings.AcquireFilesSettings(connection)
                oSettings.DefaultAcquisitionOption = VDF.Vault.Settings.AcquireFilesSettings.AcquisitionOption.Download
                oSettings.AddEntityToAcquire(oFileIteration)

                'do the download
                connection.FileManager.AcquireFiles(oSettings)
            End If

        Catch ex As Exception
            Debug.WriteLine("Could not download file")
        Finally
            VDF.Vault.Library.ConnectionManager.LogOut(connection)
        End Try

    End Sub

    Private Function GetFileIteration(nameOfFile As String, connection As VDF.Vault.Currency.Connections.Connection) _
        As VDF.Vault.Currency.Entities.FileIteration

        Dim conditions As ACW.SrchCond()
        ReDim conditions(0)

        Dim lCode As Long = 1
        Dim Defs As ACW.PropDef() = connection.WebServiceManager.PropertyService.GetPropertyDefinitionsByEntityClassId("FILE")

        Dim Prop As ACW.PropDef = Nothing

        For Each def As ACW.PropDef In Defs
            If def.DispName = "File Name" Then
                Prop = def
            End If
        Next def

        'store path in new string
        Dim pathname As String = nameOfFile.Substring(0, nameOfFile.LastIndexOf("\") + 1)
        Debug.WriteLine(pathname)

        'remove path from nameOfFile
        Dim filename As String = nameOfFile.Substring(nameOfFile.LastIndexOf("\") + 1, nameOfFile.Length - pathname.length)
        Debug.WriteLine(filename)

        'convert local path string to vault format
        Dim VaultPath As String = pathname.Substring(0, pathname.Length - 1)
        VaultPath = VaultPath.Replace("C:\VaultWork\", "$/")
        'flip the slashes
        VaultPath = VaultPath.Replace("\", "/")
        Debug.WriteLine(VaultPath)

        Dim DocService As Autodesk.Connectivity.WebServices.DocumentService
        Dim myFolder As Folder
        Dim myFolderId As Long
        Try
            DocService = New Autodesk.Connectivity.WebServices.DocumentService()
            myFolder = DocService.GetFolderByPath(VaultPath)
            myFolderId = myFolder.Id
        Catch ex As Exception
            Debug.WriteLine(ex.Message)
        End Try

        Debug.WriteLine(myFolderId)

        Dim myFolderIds(0) As Long
        myFolderIds(0) = myFolderId

        Dim searchCondition As ACW.SrchCond = New ACW.SrchCond()
        searchCondition.PropDefId = Prop.Id
        searchCondition.PropTyp = ACW.PropertySearchType.SingleProperty
        searchCondition.SrchOper = lCode

        'searchCondition.SrchTxt = nameOfFile 'nameofFile includes path
        searchCondition.SrchTxt = filename 'filename does not include path

        conditions(0) = searchCondition

        'search for files
        Dim FileList As List(Of Autodesk.Connectivity.WebServices.File) = New List(Of Autodesk.Connectivity.WebServices.File)()
        Dim sBookmark As String = String.Empty

        Dim Status As ACW.SrchStatus = Nothing
        While (Status Is Nothing OrElse FileList.Count < Status.TotalHits)
            Dim files As Autodesk.Connectivity.WebServices.File() = connection.WebServiceManager.DocumentService. _
                        FindFilesBySearchConditions(conditions, Nothing, myFolderIds, True, True, sBookmark, Status)
            If (Not files Is Nothing) Then
                FileList.AddRange(files)
            End If
        End While

        Dim oFileIteration As VDF.Vault.Currency.Entities.FileIteration = New VDF.Vault.Currency.Entities.FileIteration(connection, FileList(0))

        Return oFileIteration
    End Function

 

3 REPLIES 3
Message 2 of 4
Redmond.D
in reply to: rusmwb

Here is how I suggest writing the function...

 

Once you have the Vault path of the file (ex.  $/_Designs/Folder1/Filename.ipt), call connection.WebServiceManager.DocumentService.FindLatestFilesByPaths to get the File objects.  It's easier than using search conditions.

 

Next, create a new FileIteration object for that File and use AcquireFiles to perform the dowload.

 

 

Also, next time you get a SoapException, you can use Autodesk.DataManagement.Client.Framework.Library.ExceptionParser to extract more details about what went wrong.

 

 

 

 

 



Doug Redmond
Software Engineer
Autodesk, Inc.

Message 3 of 4
rusmwb
in reply to: Redmond.D

Doug, Thank you for the reply. I have revised my code, but I'm still missing something. Please review and advise.

Private Sub DownloadFile(fullfilename As String)
        Dim results As VDF.Vault.Results.LogInResult = VDF.Vault.Library.ConnectionManager.LogIn("rusgrveng1", _
                                                                                                 "gveng", _
                                                                                                 "username", _
                                                                                                 "password", _
                                                                                                 VDF.Vault.Currency.Connections.AuthenticationFlags.Standard, Nothing)
        If Not results.Success Then
            Return 'exit sub
        End If

        Dim connection As VDF.Vault.Currency.Connections.Connection = Nothing
        connection = results.Connection

        Try
            Dim oFileIteration As VDF.Vault.Currency.Entities.FileIteration = Nothing

            Try
                'convert local path string to vault format
                Dim VaultPath As String = fullfilename
                VaultPath = VaultPath.Replace("C:\VaultWork\", "$/")
                'flip the slashes
                VaultPath = VaultPath.Replace("\", "/")

                Dim VaultPaths() As String = New String() {VaultPath}

                Dim DocService As ACW.DocumentService
                DocService = New ACW.DocumentService()

                Dim files As ACW.File()
                files = DocService.FindLatestFilesByPaths(VaultPaths)

                Dim file As ACW.File
                file = files(0)

                oFileIteration = New VDF.Vault.Currency.Entities.FileIteration(connection, file)
            Catch ex As Exception
                Debug.WriteLine("Could not get file iteration" & ex.Message)
            End Try

            If oFileIteration IsNot Nothing Then
                'get settings
                Dim oSettings As VDF.Vault.Settings.AcquireFilesSettings = New VDF.Vault.Settings.AcquireFilesSettings(connection)
                oSettings.DefaultAcquisitionOption = VDF.Vault.Settings.AcquireFilesSettings.AcquisitionOption.Download
                oSettings.AddEntityToAcquire(oFileIteration)

                'do the download
                connection.FileManager.AcquireFiles(oSettings)
            End If

        Catch ex As Exception
            Debug.WriteLine("Could not download file")
        Finally
            VDF.Vault.Library.ConnectionManager.LogOut(connection)
        End Try

    End Sub

 I saw in another post where you advised the user to set the security header for the document service object and copy the userid and ticket values from security service headervalue to all the other services. Do I need to do that? If yes, please elaborate.

Message 4 of 4
Redmond.D
in reply to: rusmwb

You shouldn't be creating new service objects.  They are already built and managed by the Connection object.  So you call connection.WebServiceManager.DocumentService whenever you want to use the Document Service.

 



Doug Redmond
Software Engineer
Autodesk, Inc.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report