This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Add File to Existing Object

Hi,

I would like to add a template file to existing object in specific step of the workflow using VB Scripting, all examples I’ve seen here are about creating new objects, but in my I case I want to add new file to existing object and keep the old existing files without overwriting them.

Looking forward,

  • I managed to get it working this way, hopefully that might help others.

    Option Explicit
    
    ' This is mandatory to be able to add files, otherwise exception will raise.
    
    Dim oCheckedOutFiles: Set oCheckedOutFiles = Vault.ObjectFileOperations.GetFilesForModificationInEventHandler(ObjVer)
    
    ' Special folder value for TemporaryFolder, from https://msdn.microsoft.com/en-us/library/a72y2t1c(v=vs.84).aspx.
    Const TemporaryFolder = 2
        
    ' Create a filesystemobject to help working with files.
    Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    ' Get a reference to the temporary folder.
    Dim strTempFolderPath: strTempFolderPath = objFSO.GetSpecialFolder(TemporaryFolder).Path
    
    Dim oTemplateObjects: Set oTemplateObjects = GetTemplateObjects()
    Dim oTemplateObject
    For Each oTemplateObject In oTemplateObjects
    	
        ' Get the files for the current objver.
    	Dim objFiles: Set objFiles = Vault.ObjectFileOperations.GetFiles(oTemplateObject.ObjVer)
     
        
    	' Create our collection to return.
    	Dim objSourceFiles: Set objSourceFiles = CreateObject("MFilesAPI.SourceObjectFiles")
     
    	' Iterate over the files and download each in turn.
    	Dim intCounter, objFile
    	For intCounter = 1 To objFiles.Count
    		
    		Set objFile = objFiles.Item(intCounter)
    	
    		' Full download path including the file name and extension
    		Dim strTemporaryFilePath: strTemporaryFilePath = objFSO.BuildPath(strTempFolderPath, objFSO.GetTempName()) & "." & objFile.Extension
     
    		' Download the file.
    		Vault.ObjectFileOperations.DownloadFile objFile.ID, objFile.Version, strTemporaryFilePath
    
            ' Upload the file to the object 
            Vault.ObjectFileOperations.AddFile ObjVer,objFile.Title,objFile.Extension, strTemporaryFilePath
    
    	Next
    
    Next
    
    Function GetTemplateObjects()
    
        ' Search for Template having the same class.
        Dim oSearchCondition : Set oSearchCondition = CreateObject("MFilesAPI.SearchCondition")
        Dim oSearchConditions : Set oSearchConditions = CreateObject("MFilesAPI.SearchConditions")
    
        ' Class = Credit Committee Meeting 69
        oSearchCondition.ConditionType = MFConditionTypeEqual
        oSearchCondition.Expression.DataPropertyValuePropertyDef = MFBuiltInPropertyDefClass
        oSearchCondition.TypedValue.SetValue MFDatatypeLookup, 69
        oSearchConditions.Add -1, oSearchCondition
    
        ' Is template = True
        oSearchCondition.ConditionType = MFConditionTypeEqual
        oSearchCondition.Expression.DataPropertyValuePropertyDef = MFBuiltInPropertyDefIsTemplate
        oSearchCondition.TypedValue.SetValue MFDatatypeBoolean, True
        oSearchConditions.Add -1, oSearchCondition
    
        oSearchCondition.ConditionType = MFConditionTypeEqual
        oSearchCondition.Expression.DataStatusValueType = MFStatusTypeDeleted
        oSearchCondition.TypedValue.SetValue MFDatatypeBoolean, False
        oSearchConditions.Add -1, oSearchCondition
    
        ' Invoke the search operation.
        Set GetTemplateObjects = Vault.ObjectSearchOperations.SearchForObjectsByConditions(oSearchConditions, MFSearchFlagNone, false)
    
    End Function