D365 FO : DOWNLOAD A SHAREPOINT FILE USING X++

Hey Folks ! I had a requirement in Microsoft Dynamics 365 for finance and operations where I want to save & download a file from a SHAREPOINT site and from its folder using X++ code. Here i am using classes –

Microsoft.Dynamics.AX.Framework.FileManagement.SharePointDocumentStorageProvider
Microsoft.Dynamics.AX.Framework.FileManagement.DocumentContents
Microsoft.Dynamics.AX.Framework.FileManagement.DocumentLocation
Microsoft.Dynamics.AX.Framework.FileManagement.SharePointDocumentStorageProvider

Get SharePoint - Microsoft Store

Sample Code :

public static container get(container _parms)
 {
   str url = https://XXXXXXXX.sharepoint.com ;
   str siteName;
   str documentFolder = "Shared%20Documents/MyFolder/";
   str fileName = "abc.jpg";
   [url, siteName, documentFolder, fileName] = _parms;
   System.Byte[] reportBytes = new System.Byte[0]();
   System.UriBuilder builder = new System.UriBuilder(url);
   str extId = xUserInfo::getExternalId();
   Microsoft.Dynamics.AX.Framework.FileManagement.SharePointDocumentStorageProvider provider;
   Microsoft.Dynamics.AX.Framework.FileManagement.DocumentContents documentContents;
   Microsoft.Dynamics.AX.Framework.FileManagement.DocumentLocation documentLocation = new Microsoft.Dynamics.AX.Framework.FileManagement.DocumentLocation();
     provider = new Microsoft.Dynamics.AX.Framework.FileManagement.SharePointDocumentStorageProvider("XXXXXXXX.sharepoint.com", siteName, documentFolder, extId);
   System.Uri uri = new System.Uri(url + "/" + siteName + "/" + documentFolder + "/" + fileName);
   System.Uri uri1 = new System.Uri(url + "/" + siteName + "/_api/Web/GetFileByServerRelativePath(decodedurl='/" + siteName + "/" + documentFolder + "/" + fileName + "')");
   siteName = sites/MyGallery
   documentLocation.NavigationUri = uri;
   documentLocation.AccessUri = uri1;
   documentContents = provider.GetFile(documentLocation);
   container containerGet;
   if (documentContents)
   {
       System.IO.MemoryStream memoryStream2 = new System.IO.MemoryStream();
       System.IO.Stream stream12 = documentContents.Content;
       stream12.CopyTo(memoryStream2);
       System.Byte[]           bytes112 = new System.Byte[0]();
       bytes112 = memoryStream2.ToArray();
       System.IO.MemoryStream memoryStream122 = new System.IO.MemoryStream(bytes112);
       containerGet = Binary::constructFromMemoryStream(memoryStream2).getContainer();
   }
   return containerGet;
 }

3 thoughts on “D365 FO : DOWNLOAD A SHAREPOINT FILE USING X++”

  1. I have a similar requirement to Read Excel file from SharePoint folder to Microsoft dynamics D365FO. How can it be done ?

  2. I have a similar requirement to Read multiple Excel files from SharePoint folder to Microsoft dynamics D365FO. How can it be achieved?

Leave a Reply