NAV2016 NAS copy / read file from network drive

rsaritzkyrsaritzky Member Posts: 469
Hi all,

This question has been asked before in a variety of flavors, but I haven't found this particular scenario with an answer.

We have a codeunit running in the NAV2016 job queue. The intent is to read a file on a network share, e.g.

\\servername\Scanfolder\sub\sub\thisfile.txt

It then parses that text file and puts some data in the NAV database.

The issue is that the server cannot find/read this file.

The userid that the service tier is running under has full Active Directory permissions to the \\servername\scanfolder. I can log into the server with this same userid and see the directory and open the file.

If I run the codeunit under the NAV client, this code fails:
FileName := '\\servername\data$\Scanned Documents\Logs\CD_NAV_Import.txt';
IF ScanFile.OPEN(FileName) THEN BEGIN
  IF CONFIRM('Was able to open ' + FileName) THEN;
  ScanFile.CLOSE;
END
ELSE
  IF CONFIRM('Could not open file ' + FileName) THEN;  <---Fails
EXIT;


If I use the File management codeunit functions, it works when running from a NAV client:
IF NOT FileMgt.ClientFileExists(FileName) THEN
  EXIT;
TempFileName := FileMgt.UploadFileSilent(FileName);
IF ScanFile.OPEN(TempFileName) THEN ;

However, when running under a NAS session, the above code fails with this error:
Microsoft Dynamics NAV Server attempted to issue a client callback to create a DotNet object: System.IO.File (CodeUnit 419 File Management). Client callbacks are not supported on Microsoft Dynamics NAV Server.

Does anyone have working code that runs under a NAS sessions that reads data from a file on a network share? I don't mind having to copy it to a temp file if necessary, but I haven't found the right combination of methods to do this. I have also been told that the "delegation" method is not necessary for NAV2016 (that it applies only to 2009). I've also tried setting the delegation properties mentioned in other threads, but still no luck.

Thanks in advance,

Ron
Ron

Best Answer

  • rsaritzkyrsaritzky Member Posts: 469
    Answer ✓
    I tried a few more functions and got this code to work:
    FileName := '\\servername\data$\Scanned Documents\Logs\CD_NAV_Import.txt';
    IF FileMgt.ServerFileExists(FileName) THEN BEGIN
      TempFileName := FileMgt.ServerTempFileName('');
      FileMgt.CopyServerFile(FileName,TempFileName,TRUE);
      ScanFile.WRITEMODE(FALSE);
      ScanFile.TEXTMODE(TRUE);
      IF NOT ScanFile.OPEN(TempFileName) THEN
      ...[rest of processing]
    END;
    
    Ron

Answers

  • JuhlJuhl Member Posts: 724
    Set your dotnet variables to NOT run on client.
    Follow me on my blog juhl.blog
  • rsaritzkyrsaritzky Member Posts: 469
    I'm not sure what you mean here...I'm using the functions in CU419 "as is". I have no DotNet variables defined explicitly.
    Ron
  • JuhlJuhl Member Posts: 724
    Then you have a gui instance in your code.
    If you have a dialog og message in you code, then it fails. NAS has no GUI
    Follow me on my blog juhl.blog
  • rsaritzkyrsaritzky Member Posts: 469
    I am trying to use the functions in CU419 "As is" - there are no dialogs or MESSAGE commands in the functions I am trying, but I'm looking for a way to (a) locate a file on a network drive (b) copy it to the server (if necessary), then process it. So far, I cannot find/open a file, even though the service tier userid has permissions to that network resource. I'm looking for actual code samples.

    Thanks

    Ron
    Ron
  • JuhlJuhl Member Posts: 724
    Ahh, read your code again. You are using client to server functions, NAS is not a client.
    Follow me on my blog juhl.blog
  • rsaritzkyrsaritzky Member Posts: 469
    Answer ✓
    I tried a few more functions and got this code to work:
    FileName := '\\servername\data$\Scanned Documents\Logs\CD_NAV_Import.txt';
    IF FileMgt.ServerFileExists(FileName) THEN BEGIN
      TempFileName := FileMgt.ServerTempFileName('');
      FileMgt.CopyServerFile(FileName,TempFileName,TRUE);
      ScanFile.WRITEMODE(FALSE);
      ScanFile.TEXTMODE(TRUE);
      IF NOT ScanFile.OPEN(TempFileName) THEN
      ...[rest of processing]
    END;
    
    Ron
Sign In or Register to comment.