Forum Discussion

BenStuart's avatar
BenStuart
New Contributor III
2 years ago

Move File from Application Database to External Shared Drive

I currently have a metadata file being created by an Extender BR and saved down to the Application Database:

 

Case Is = ExtenderFunctionType.ExecuteDataMgmtBusinessRuleStep
	
'Export File
Dim fileName As String = "AccessoryMetadata.csv"
Dim filePath As String = $"Documents/Public/XFDocs/XFDocs_Public"
Dim ud1DimPk As DimPk = BRApi.Finance.Dim.GetDimPk(si, "MainProduct")
Dim category As String = ""
Dim businessStream As String = ""
'Export CSV to User Temp Folder
Dim listOfParents As List(Of memberinfo) = BRApi.Finance.Members.GetMembersUsingFilter(si, _
	ud1DimPk, _
		"U1#ReportingProduct.Descendants.Where(" & _	
		"Name Contains Accessories" & _
		"And Name <> 15_Accessories" & _
		"And HasChildren = True)", _
	True)
Dim csv As New Text.StringBuilder
csv.AppendLine("Name, Description, ParentName, Category, Business Stream")
For Each parentMember In listOfParents
	For Each childMember In BRApi.Finance.Members.GetChildren(si, _
			ud1DimPk, parentMember.Member.MemberId)
		...

		csv.AppendLine($"""{childMember.Name}"",""{childMember.Description _
			}"",""{parentMember.Member.Name}"",""{category}"",""{businessStream}""")
	
	Next
Next

Dim fileBytes As Byte() = Encoding.UTF8.GetBytes(csv.ToString)
'Save csv to file
Dim XFfileDataInfo As New XFFileInfo(FileSystemLocation.ApplicationDatabase, fileName, filePath)
Dim XFfileData As New XFFile(XFFileDataInfo, String.Empty, fileBytes)
brapi.FileSystem.InsertOrUpdateFile(si, XFfileData)

 

Is there any way to then automatically move this file to an external shared drive? Or does the above need to be written in a different way in order for it to work?

  • You can move any file from onestream to external shared drive.

    '1. Create your session here
    Dim sessionOptions As New SessionOptions
    		        With sessionOptions
    		            .Protocol = WinSCP.Protocol.Sftp
    					.Portnumber = 22
    		            .HostName = ""
    				   .UserName = ""
    				   .PrivateKeyPassphrase = ""
    		            .SshHostKeyFingerprint = ""
    		            .SshPrivateKeyPath = ""
    					
    				  End With
    				  
    				  
    '2. Send file using the session
    
    				FileName = "FileName.csv"
    			    Using session As New Session
    		            ' Connect
    		            session.Open(sessionOptions)
    					Dim LocalDirectory As String = BRApi.FileSystem.GetFileShareFolder(si, FileShareFolderTypes.BatchHarvest, api)
    					Dim Dest_folder As String =""
    					session.PutFiles(LocalDirectory & FileName, Dest_folder & FileName)
    					
    		        End Using
  • OS_Pizza's avatar
    OS_Pizza
    Contributor III

    You can move any file from onestream to external shared drive.

    '1. Create your session here
    Dim sessionOptions As New SessionOptions
    		        With sessionOptions
    		            .Protocol = WinSCP.Protocol.Sftp
    					.Portnumber = 22
    		            .HostName = ""
    				   .UserName = ""
    				   .PrivateKeyPassphrase = ""
    		            .SshHostKeyFingerprint = ""
    		            .SshPrivateKeyPath = ""
    					
    				  End With
    				  
    				  
    '2. Send file using the session
    
    				FileName = "FileName.csv"
    			    Using session As New Session
    		            ' Connect
    		            session.Open(sessionOptions)
    					Dim LocalDirectory As String = BRApi.FileSystem.GetFileShareFolder(si, FileShareFolderTypes.BatchHarvest, api)
    					Dim Dest_folder As String =""
    					session.PutFiles(LocalDirectory & FileName, Dest_folder & FileName)
    					
    		        End Using
    • BenStuart's avatar
      BenStuart
      New Contributor III

      Hi OS_Pizza,

      Does this require me to import any Namespaces? I tried adding the above to my existing Extender rule but it said that SessionOptions was not defined.

      Many thanks

      • OS_Pizza's avatar
        OS_Pizza
        Contributor III

        Absolutely ! Forgot to mention

        Imports WinSCP
  • RobbSalzmann's avatar
    RobbSalzmann
    Valued Contributor II

    BenStuart If the shared drive is mapped to a local drive letter on the server where OS is running, you write directly to it like this:
    File.WriteAllBytes("Z:\OneStreamShare\FileShare\data.csv", System.Text.Encoding.Default.GetBytes(csv.ToString()))