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 to an idw file.
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.
Download Vault_list_Add_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”.
From the attached project:
PrivateSub 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)
IfNot results.Success Then
Return
EndIf
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 AsString =
"C:\Temp\myFile_test.xlsx"
Dim myFldrCol As System.Collections.Generic.List _
(Of VDF.Vault.Currency.Entities.Folder)
myFldrCol = connection.FolderManager. _
GetChildFolders(connection.FolderManager.RootFolder,
False, False)
Dim myFolder As _
VDF.Vault.Currency.Entities.Folder =
CType(myFldrCol.ElementAt(2),
VDF.Vault.Currency.Entities.Folder)
Dim myFileIterationNewFile As _
VDF.Vault.Currency.Entities.FileIteration = _
Nothing
Using fileStream AsStream = NewFileStream _
(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)
EndUsing
IfNot myFileIterationNewFile IsNothingThen
Dim bAddedAttachment AsBoolean = False
'Need to change this string to an existing
' file in the Vault
Dim strNameOfFileToAddTo =
"wb_testing_vault_UDPs_2.idw"
Dim fldrId AsLong =
myFileIterationNewFile.FolderId
bAddedAttachment = AddMyAttachment _
(strNameOfFileToAddTo,
myFileIterationNewFile.EntityIterationId,
fldrId, connection)
If bAddedAttachment = FalseThen
MessageBox.Show _
("Unable to get FileIteration object")
Else
MessageBox.Show _
("Success - Added and attached file")
EndIf
EndIf
'Logout
VDF.Vault.Library.ConnectionManager.LogOut _
(connection)
EndSub
PublicFunction AddMyAttachment _
(nameOfFileToAttachTo AsString,
myFileIterationId AsLong,
myFolderIdOfNewFileIteration AsLong,
connection As _
VDF.Vault.Currency.Connections.Connection) _
AsBoolean
Dim oFileIteration As _
VDF.Vault.Currency.Entities.FileIteration =
getFileIteration(nameOfFileToAttachTo, connection)
If oFileIteration IsNothingThen
MessageBox.Show("Unable to get FileIteration")
ReturnFalse
EndIf
' 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 = TrueThen
' settings used in GetFileAssociationLites()
Dim myFileRelationshipSettings As _
VDF.Vault.Settings.FileRelationshipGatheringSettings
myFileRelationshipSettings = _
New VDF.Vault.Settings.FileRelationshipGatheringSettings
myFileRelationshipSettings. _
IncludeAttachments = True
Dim myColOfFileAssocLite As _
System.Collections.Generic.IEnumerable _
(Of ACW.FileAssocLite) = Nothing
myColOfFileAssocLite =
connection.FileManager.GetFileAssociationLites _
(NewLong() {oNewFileIteration.EntityIterationId},
myFileRelationshipSettings)
' Going to add new FileAssocParam
' objects to this list
' ArrayList to contain
' FileAssocParam objects
Dim fileAssocParams AsArrayList =
NewArrayList
' Add FileAssocParam objects to the ArrayList
' using values from the collection
' of FileAssocLite in the collection
' returned from GetFileAssociationLites()
IfNot myColOfFileAssocLite IsNothingThen
Dim myFileAssocLite AsFileAssocLite
' 'Go through each FileAssoLite in the
' in the collection of FileAssocLite
ForEach myFileAssocLite In
myColOfFileAssocLite
' This is a new FileAssocParam that
' is going to be added to the List
' getting the properties
Dim par AsFileAssocParam =
NewFileAssocParam()
par.CldFileId =
myFileAssocLite.CldFileId
par.RefId = myFileAssocLite.RefId
par.Source = myFileAssocLite.Source
par.Typ = myFileAssocLite.Typ
par.ExpectedVaultPath =
myFileAssocLite.ExpectedVaultPath
fileAssocParams.Add(par)
Next
EndIf
' Get the folder of the file
' we are associating
Dim myDictionary AsIDictionary _
(OfLong, VDF.Vault.Currency.Entities.Folder)
myDictionary =
connection.FolderManager.GetFoldersByIds _
(NewLong() {myFolderIdOfNewFileIteration})
' Get the folder from the dictionary
Dim keyPair As Generic.KeyValuePair _
(OfLong, 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 = NewFileAssocParam()
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 AsFileAssocParam() _
= 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 _
(oFileIteration, "wbTesting", False,
Date.Now,
myFileAssocParamArray,
Nothing, False, Nothing,
ACW.FileClassification.None,
False, myStream)
Else
MessageBox.Show("Unable to check out")
ReturnFalse
EndIf
ReturnTrue
EndFunction