Vault 2014 API example that Adds a file and associates it to an existing file in the vault

By Wayne Brill

You may need to add a file and associate it to a file that already exists in the vault. This VB.NET example shows how this can be done. The example as I tested it adds an excel file on disk to a text file in the vault.

connection.FileManager.AddFile() is used to add a file from the disk to the vault.

connection.FileManager.AcquireFiles() is used to check out the file that is going to have another file attached to it. (it is checked out without being downloaded).

connection.FileManager.GetFileAssociationLites() is used to get the existing file associations. A new FileAssocParam is used to add the attachment. The associated files are added as a parameter to the connection.FileManager.CheckinFile method.

The CheckinFile method has two signatures. The one with the System.IO.Stream bytes parameter is used to check in a file that was not downloaded. (the parameter is Nothing)

The connection object is an: Autodesk.DataManagement.Client.Framework.Vault.Currency.Connections.Connection

You could use the WebService function GetFileAssociationsByIds to get the associated files. One of my colleagues in engineering has found that GetFileAssociationLites() works better if there are a large number of associated files and is used in this example. (GetFileAssociationsByIds() is in the example as comments)

Note: Currently the Vault API does not support adding cad files such as Inventor Assemblies. I have also found that attaching a file to an .idw can adversely effect the dwf file that is attached as a design visualization file. (the dwf is gone as an attachment and needs to be recreated – this happens when you check in the idw using the Vault Inventor Add-In) Also the Inventor Vault Add-In will list the attached files if you check out all. The attached files may not be downloaded however and this causes an error on check in.  

 
Download Vault_list_Add_and_Associate

The example is an update to VaultList SDK sample. It has three new buttons. One of the buttons will do the “add and attach file”.

image

From the attached project:

Private Sub Button4_Click(sender As System.Object,
       e As System.EventArgs) Handles Button4.Click
 
    ' For demonstration purposes, the information
    ' is hard-coded.
    Dim results As VDF.Vault.Results.LogInResult =
        VDF.Vault.Library.ConnectionManager.LogIn _
        ("localhost", "Vault", "Administrator", "",
        VDF.Vault.Currency.Connections. _
                  AuthenticationFlags.Standard,
         Nothing)
 
    If Not results.Success Then
        Return
    End If
 
    Dim connection As  _
    VDF.Vault.Currency.Connections.Connection _
                          = results.Connection
 
    ' Need to change this string to a file on 
    ' a drive that you want to add to the vault
    Dim filePath As String =
                      "C:TempmyFile_20.xlsx"
 
 
    Dim myFldrCol As System.Collections.Generic.List _
               (Of VDF.Vault.Currency.Entities.Folder)
    myFldrCol = connection.FolderManager. _
 GetChildFolders(connection.FolderManager.RootFolder,
                                       False, False)
 
 
    ' Get the folder to add the new file to change 
    ' the FullName test to a Folder in your vault
    Dim myFolder As  _
    VDF.Vault.Currency.Entities.Folder = Nothing
    For Each Flder As  _
    VDF.Vault.Currency.Entities.Folder In myFldrCol
        If Flder.FullName = "$/wB_Excel_Files" Then
            myFolder = Flder
            Exit For
        End If
    Next
 
    Dim myFileIterationNewFile As  _
    VDF.Vault.Currency.Entities.FileIteration = _
                                           Nothing
 
    Using fileStream As Stream = New FileStream _
       (filePath, FileMode.Open, FileAccess.Read)
 
        ' Add the file to the vault
        myFileIterationNewFile =
            connection.FileManager.AddFile(myFolder,
            Path.GetFileName(filePath),
            "Added by wB code",
            DateTime.Now, Nothing, Nothing,
            ACW.FileClassification.None,
            False, fileStream)
 
    End Using
 
    If Not myFileIterationNewFile Is Nothing Then
        Dim bAddedAttachment As Boolean = False
        'Need to change this string to an existing
        ' file in the Vault
        Dim strNameOfFileToAddTo = "wB_test.txt"
 
        Dim fldrId As Long =
               myFileIterationNewFile.FolderId
 
        bAddedAttachment = AddMyAttachment _
            (strNameOfFileToAddTo,
             myFileIterationNewFile.EntityIterationId,
             fldrId, connection)
 
        If bAddedAttachment = False Then
            MessageBox.Show _
         ("Unable to get FileIteration object")
        Else
            MessageBox.Show _
          ("Success - Added and attached file")
        End If
    End If
 
    'Logout
    VDF.Vault.Library.ConnectionManager.LogOut _
                                    (connection)
End Sub
 
 
Public Function AddMyAttachment _
                 (nameOfFileToAttachTo As String,
                       myFileIterationId As Long,
            myFolderIdOfNewFileIteration As Long,
            connection As  _
    VDF.Vault.Currency.Connections.Connection) _
                                      As Boolean
    Dim oFileIteration As  _
    VDF.Vault.Currency.Entities.FileIteration =
getFileIteration(nameOfFileToAttachTo, connection)
 
    If oFileIteration Is Nothing Then
        MessageBox.Show("Unable to get FileIteration")
        Return False
    End If
 
    ' Get settings 
    Dim oSettings As  _
        VDF.Vault.Settings.AcquireFilesSettings =
      New VDF.Vault.Settings.AcquireFilesSettings _
                                       (connection)
 
    ' Going to Check Out (not download file)
    oSettings.DefaultAcquisitionOption =
        VDF.Vault.Settings.AcquireFilesSettings. _
                        AcquisitionOption.Checkout
 
    ' add the file to the settings
    oSettings.AddEntityToAcquire(oFileIteration)
 
    'Do the CheckOut
    Dim myAcquireVaultSettings As  _
        VDF.Vault.Results.AcquireFilesResults
    myAcquireVaultSettings =
  connection.FileManager.AcquireFiles(oSettings)
 
    Dim oNewFileIteration As  _
        VDF.Vault.Currency.Entities.FileIteration
 
    Dim myFileAcqRes As  _
        VDF.Vault.Results.FileAcquisitionResult
 
    myFileAcqRes =
          myAcquireVaultSettings.FileResults(0)
 
    oNewFileIteration =
                   myFileAcqRes.NewFileIteration
 
    If oNewFileIteration.IsCheckedOut = True Then
 
        ' settings used in GetFileAssociationLites()
        Dim myFileRelationshipSettings As  _
VDF.Vault.Settings.FileRelationshipGatheringSettings
        myFileRelationshipSettings = _
New VDF.Vault.Settings.FileRelationshipGatheringSettings
 
        myFileRelationshipSettings. _
                            IncludeAttachments = True
        myFileRelationshipSettings. _
                               IncludeChildren = True
        myFileRelationshipSettings. _
                                IncludeParents = True
        myFileRelationshipSettings. _
                   IncludeRelatedDocumentation = True
 
        Dim myColOfFileAssocLite As  _
            System.Collections.Generic.IEnumerable _
                   (Of ACW.FileAssocLite) = Nothing
 
        myColOfFileAssocLite =
    connection.FileManager.GetFileAssociationLites _
  (New Long() {oNewFileIteration.EntityIterationId},
                        myFileRelationshipSettings)
 
        ' Going to add new FileAssocParam 
        ' objects to this list 
 
        ' ArrayList to contain 
        ' FileAssocParam objects
        Dim fileAssocParams As ArrayList =
                                New ArrayList
 
        ' Add FileAssocParam objects to the ArrayList
        ' using values from the collection 
        ' of FileAssocLite in the collection 
        ' returned from GetFileAssociationLites()
        If Not myColOfFileAssocLite Is Nothing Then
            Dim myFileAssocLite As FileAssocLite
            '    'Go through each FileAssoLite in the
            '   in the collection of FileAssocLite
            For Each myFileAssocLite In
                               myColOfFileAssocLite
                ' This is a new FileAssocParam that 
                ' is going to be added to the List
                ' getting the properties 
                Dim par As FileAssocParam =
                               New FileAssocParam()
                par.CldFileId =
                         myFileAssocLite.CldFileId
                par.RefId = myFileAssocLite.RefId
                par.Source = myFileAssocLite.Source
                par.Typ = myFileAssocLite.Typ
                par.ExpectedVaultPath =
                   myFileAssocLite.ExpectedVaultPath
                fileAssocParams.Add(par)
            Next
        End If
 
 
        ' Get the folder of the file 
        ' we are associating
        Dim myDictionary As IDictionary _
       (Of Long, VDF.Vault.Currency.Entities.Folder)
        myDictionary =
         connection.FolderManager.GetFoldersByIds _
        (New Long() {myFolderIdOfNewFileIteration})
 
        ' Get the folder from the dictionary
        Dim keyPair As Generic.KeyValuePair _
     (Of Long, VDF.Vault.Currency.Entities.Folder)
 
        keyPair = myDictionary.First
        Dim myFldr As  _
            VDF.Vault.Currency.Entities.Folder _
                                  = keyPair.Value
 
        ' Add the new association
        Dim newFileAssocPar As  _
        FileAssocParam = New FileAssocParam()
        newFileAssocPar.CldFileId = _
                            myFileIterationId
        newFileAssocPar.RefId = Nothing
        newFileAssocPar.Source = Nothing
        newFileAssocPar.Typ = _
                    AssociationType.Attachment
        newFileAssocPar.ExpectedVaultPath = _
                            myFldr.FolderPath
 
 
        ' Add our new FileAssocParam
        fileAssocParams.Add(newFileAssocPar)
 
        ' Populate this with the FileAssocParam
        ' objects that we create from properties'
        ' in the FileAssocArray and the new file
        Dim myFileAssocParamArray As FileAssocParam() _
            = DirectCast(fileAssocParams.ToArray _
          (GetType(FileAssocParam)), FileAssocParam())
 
        ' Use the overloaded method of CheckInFile 
        ' that takes a stream This will allow 
        ' checking in a file that has not been
        ' downloaded
        ' (by passing in a stream that is Nothing) 
        Dim myStream As System.IO.Stream = Nothing
        connection.FileManager.CheckinFile _
            (oNewFileIteration, "wbTesting", False,
                                       Date.Now,
                          myFileAssocParamArray,
                          Nothing, False, Nothing,
                      ACW.FileClassification.None,
                                  False, myStream)
 
    Else
        MessageBox.Show("Unable to check out")
        Return False
    End If
 
    Return True
 
End Function

Comments

3 responses to “Vault 2014 API example that Adds a file and associates it to an existing file in the vault”

  1. Hi,
    I am looking for some help with Vault 2014 API for custom objects. What I am trying to achieve is that, I have created a custom object in C# and I would like to add the property instance from a windows form where the user can input a value this then is displayed under the property definition sysname column heading for that custom object. Can I achieve this using PropInst.val() call? Any help you be most appreciated.

  2. Wayne Brill Avatar
    Wayne Brill

    Hi Barry,
    Please post this question in the Vault Customization newsgroup. Myself or one of my colleages will ensure that it is replied to.
    http://forums.autodesk.com/t5/Vault-Customization/bd-p/301
    Thanks,
    Wayne

  3. Gautam K Avatar
    Gautam K

    Hi,
    Does Vault 2018 supports adding cad files such as Inventor parts/assemblies ?
    Thanks

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading