Business Central to Azure File Shares - Save your files14-10-2024 |
Save files to Azure File Shares from Business Central Learn about Azure Storage Account If you want to store your files to Azure File Shares, please follow below steps: Create a setup table for Azure Storage Account. table 50102 "Azure Storage Setup" { DataClassification = CustomerContent;
fields { field(1; "Primary Key"; code[20]) { DataClassification = CustomerContent; } field(2; "Account Name"; Text[50]) { Caption = 'Account Name'; DataClassification = CustomerContent; } field(3; "Container Name"; Text[150]) { Caption = 'Container Name'; DataClassification = CustomerContent; ExtendedDatatype = URL; } field(4; "Account Url"; Text[250]) { Caption = 'Account Url'; DataClassification = CustomerContent; ExtendedDatatype = URL; }
field(5; "SaS Token"; Text[250]) { Caption = 'SaS Token'; DataClassification = CustomerContent;
} field(6; "File Share"; Text[250]) { DataClassification = CustomerContent;
} }
keys { key(Key1; "Primary Key") { Clustered = true; } } } Create a setup page for the above table. page 50106 "Azure Storage Setup" { PageType = Card; ApplicationArea = All; UsageCategory = Tasks; SourceTable = "Azure Storage Setup";
layout { area(Content) { group(General) { field("Account Name"; Rec."Account Name") { } field("Container Name"; Rec."Container Name") { } field("File Share"; Rec."File Share") { } field("Account Url"; Rec."Account Url") { } field("SaS Token"; Rec."SaS Token") { } } } } actions { area(navigation) { action(test) { Caption = 'Save Test File'; Image = SendTo; Promoted = true; PromotedCategory = Process; PromotedIsBig = true; PromotedOnly = true;
trigger OnAction() var AzureStorage: Codeunit "Azure Storage"; begin AzureStorage.CreateFile('test. end; } } }
trigger OnOpenPage() begin If not rec.get then rec.Insert(); end; } Create a codeunit named "Azure Storage" to hold the below procedures. procedure CreateFile(FileName: text; content: Text) var TempBlob: Codeunit "Temp Blob"; InS: InStream; OutS: OutStream; begin TempBlob.CreateOutStream(OutS) OutS.WriteText(content); TempBlob.CreateInStream(InS); SaveToAzureFileShare(FileName, InS); end;
local procedure SaveToAzureFileShare(FileName: Text[250]; InStream: InStream) var AzureStorageSetup: Record "Azure Storage Setup"; AFSFileClient: Codeunit "AFS File Client"; AFSOperationResponse: Codeunit "AFS Operation Response"; StorageServiceAuthorization: Codeunit "Storage Service Authorization"; Authorization: Interface "Storage Service Authorization"; StorageAccount: Text[250]; Fileshare: Text[250]; SASToken: Text[250]; begin AzureStorageSetup.Get(); AzureStorageSetup.TestField(" AzureStorageSetup.TestField(" AzureStorageSetup.TestField(" SASToken := AzureStorageSetup."SaS Token"; StorageAccount := AzureStorageSetup."Account Name"; Fileshare := AzureStorageSetup."File Share";
Authorization := StorageServiceAuthorization. AFSFileClient.Initialize( AFSOperationResponse := AFSFileClient.CreateFile( if AFSOperationResponse. AFSOperationResponse := AFSFileClient.PutFileStream( if AFSOperationResponse. Message('%1 saved succesfully.', FileName); end else begin Error(AFSOperationResponse. end; end else begin Error(AFSOperationResponse. end; end; Once you do the setup, click on Save Test File button to save the file to Azure File Shares You will get a confirmation message that file has been saved. When you check Azure File Share explorer, you will see your file. When you open the file. it will display the content as below. Note: Account Url & Container name is for information purpose only and is not being used in the above code. You can change the above code to suit your setup & requirements. Give it a try! #BusinessCentral #BC #Azure #AzureStorage #AzureFileShares |