Rahul Sharma

Solutions Architect - Microsoft Dynamics AX | Azure

This is a technology blog covering Microsoft Dynamics AX, Microsoft Dynamics CRM, Microsoft Azure, IoT, .Net, SharePoint, SQL Server, SSRS, SSAS, SSIS, Apache Cassandra, MongoDB, and related technologies. Join this blog on facebook {Rahul Sharma}, to start a discussion !!! NOTE: My employer is not responsible for the content published here.

Index | MS Dynamics AX | MS Dynamics CRM
View blog as >> Magazine | Sidebar | Flipcard | Mosaic | Snapshot | Timeslide

Change File Ownership through X++ in Dynamics AX

This article shows a little trick that might be needed for testing AX AIF file adapter based integration. We all know that how frustrating it can be to setup input file ownership to current AX user while testing otherwise AX will complain about it.

Here is a small static function with filepath as a parameter. This function will set current AX user as the owner of the input file. This function can be called right before the call to fileSystem.ReadFile(filePath) in \Classes\AifFileSystemReceiveAdapter\readFile.

   1:  static client protected void SetFileOwnership2CurUser(FilePath _filePath)
   2:  {
   3:      UserInfo                                            userInfo;
   4:      System.IO.FileInfo                                  fileInfo;
   5:      System.Security.AccessControl.FileSecurity          fileSecurity;
   6:      System.Security.Principal.SecurityIdentifier        sid;
   7:      System.Security.AccessControl.AccessControlSections acSections;
   8:      ;
  10:      //select current user
  11:      select userInfo
  12:          where userInfo.Id == curuserid();
  14:      try
  15:      {
  16:          new InteropPermission(InteropKind::ClrInterop).assert();
  17:          fileInfo           = new System.IO.FileInfo(_filePath);
  18:          acSections      = System.Security.AccessControl.AccessControlSections::Owner;
  19:          fileSecurity       = fileInfo.GetAccessControl(acSections);
  20:          sid = new System.Security.Principal.SecurityIdentifier(userInfo.sid);
  21:          //set ownership
  22:          fileSecurity.SetOwner(sid);
  23:          fileInfo.SetAccessControl(fileSecurity);
  24:          CodeAccessPermission::revertAssert();
  25:      }
  26:      catch
  27:      {
  28:          throw error(AifUtil::getClrErrorMessage());
  29:      }
  30:  }

Join me on facebook and feel free to post your comments / feedback / queries.