BC 21 import multiple xmlport with dynamic filesnames

NoelineNoeline Member Posts: 54
Hi

We have written a codeunit to import via xmlports that have been defined. RequestPage is set to false, and and initial FileName is specified in the properties of the XMLPorts. The codeunit reads the import folder and imports the files, one at a time. Our problem is that although we have set the FileName on the OnInitXML and OnPreXML, the system stills pops up a window for each file asking the user the Choose the file.

If there are 20 or 50 files this user input becomes problematic. Is it possible to avoid the user having to choose a file each time, and if so, how?

Regards - Noeline

Best Answer

  • ftorneroftornero Member Posts: 522
    Answer ✓
    A little change in the procedure
        local procedure ReadFiles(DirPath: Text; pXmlPort: XmlPort "Your XMLPort")
        var
            fileMgt: Codeunit "File Management";
            NameValBuf: Record "Name/Value Buffer" temporary;
            xFile: File;
            InStr: InStream;
    
        begin
            if fileMgt.ServerDirectoryExists(DirPath) then begin
                fileMgt.GetServerDirectoryFilesList(NameValBuf, DirPath);
                if NameValBuf.FindSet() then
                    repeat
                        xFile.Open(NameValBuf.Name);
                        xfile.CreateInStream(InStr);
                        Clear(pXmlPort);
                        pXmlPort.SetSource(InStr);
                        pXmlPort.Filename := NameValBuf.Name;
                        pXmlPort.Import();
                        xFile.Close();
                    until NameValBuf.Next() = 0;
            end else
                Message('Directory %1, doesn''t exists', DirPath);
        end;
    

Answers

  • ftorneroftornero Member Posts: 522
    Hello @Noeline,

    You need to load every file to a InStream then XMLPort.SetSource(InStream) and XMLPort.Import()

    Regards.
  • NoelineNoeline Member Posts: 54
    Hello @ftornero

    Thank you. I will try it.

    Regards - Noeline
  • NoelineNoeline Member Posts: 54
    Hello @ftornero

    I forgot to mention, this is on-prem. The UploadIntoStream method opens a dialog asking the user to choose the file, and I don't find a way around it. My workaround has been to import with CSV Buffer instead. This does not open a dialog and uses the path and file variables, but it has certain limitations. I would prefer the xmlports if I could find a way to do a silent upload.

    Regards - Noeline
  • ftorneroftornero Member Posts: 522
    Hello @Noeline,

    You need to do something like this:
        local procedure ReadFiles(DirPath: Text; pXmlPort: XmlPort "Your XmlPort")
        var
            fileMgt: Codeunit "File Management";
            NameValBuf: Record "Name/Value Buffer" temporary;
            xFile: File;
            InStr: InStream;
            
        begin
            if fileMgt.ServerDirectoryExists(DirPath) then begin
                fileMgt.GetServerDirectoryFilesList(NameValBuf, DirPath);
                if NameValBuf.FindSet() then
                    repeat
                        xFile.Open(NameValBuf.Name);
                        xfile.CreateInStream(InStr);
                        pXmlPort.SetSource(InStr);
                        pXmlPort.Import();
                    until NameValBuf.Next() = 0;
            end else
                Message('Directory %1, doesn''t exists', DirPath);
        end;
    

    Regards
  • ftorneroftornero Member Posts: 522
    Answer ✓
    A little change in the procedure
        local procedure ReadFiles(DirPath: Text; pXmlPort: XmlPort "Your XMLPort")
        var
            fileMgt: Codeunit "File Management";
            NameValBuf: Record "Name/Value Buffer" temporary;
            xFile: File;
            InStr: InStream;
    
        begin
            if fileMgt.ServerDirectoryExists(DirPath) then begin
                fileMgt.GetServerDirectoryFilesList(NameValBuf, DirPath);
                if NameValBuf.FindSet() then
                    repeat
                        xFile.Open(NameValBuf.Name);
                        xfile.CreateInStream(InStr);
                        Clear(pXmlPort);
                        pXmlPort.SetSource(InStr);
                        pXmlPort.Filename := NameValBuf.Name;
                        pXmlPort.Import();
                        xFile.Close();
                    until NameValBuf.Next() = 0;
            end else
                Message('Directory %1, doesn''t exists', DirPath);
        end;
    
Sign In or Register to comment.