Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

Upload PDF file to sharepoint using X++ code in D365 F&O

(0) ShareShare
ReportReport
Posted on by 752
thread removed
  • Community member Profile Picture
    6 on at
    Upload PDF file to sharepoint using X++ code in D365 F&O
    Where the content? tiny fishing
  • Rhushikesh R Profile Picture
    752 on at
    Upload PDF file to sharepoint using X++ code in D365 F&O
    Formatted code
     
    using System.IO;
    using System.IO.Path;
    using Microsoft.Azure;
    using Blobstorage = Microsoft.WindowsAzure.Storage;
    using Microsoft.WindowsAzure.Storage.Blob;
    using Microsoft.Dynamics.Platform.Integration.SharePoint;
    using Microsoft.Dynamics.ApplicationPlatform.Services.Instrumentation;
    using Microsoft.DynamicsOnline.Infrastructure.Components.SharedServiceUnitStorage;
    using Microsoft.Dynamics.AX.Framework.FileManagement;
    
    public class ILUStoreFileToSharepoint
    {
        str docfiletype;
        Microsoft.Dynamics.AX.Framework.FileManagement.IDocumentStorageProvider storageProvider;
     
        public static ILUStoreFileToSharepoint construct()
        {
            return new ILUStoreFileToSharepoint();
        }
    
        public void upload(DocuRef _docuRef)
        {
            System.Exception            ex;
            System.IO.Stream            memoryStream;
            Filename 		            fileNameExt;
            DocuValue              	    docuValue;
            str 			            errorMessage;
      
            select firstonly docuValue
                where docuValue.RecId == _docuRef.ValueRecId;
        
            memoryStream = this.readfromAzureBlob(_docuRef);
            memoryStream.Seek(0, System.IO.SeekOrigin::Begin);
    
            fileNameExt =  docuValue.FileName + '.' + docuValue.FileType;
    
            //str folderPath = "/sites/'new site'/'Folder'";
            str folderPath = "/salesorder/SF001";
    
            str fileContentType = System.Web.MimeMapping::GetMimeMapping(fileNameExt);
    
            ISharePointProxy    proxy = null;
            DOCUPARAMETERS      docuParameters;
            str                 src = '';
            str                 hostName = '';
    
            docuParameters      = DOCUPARAMETERS::find();
    
            str                 externalId;
    
            try
            {
                ttsbegin;
                if(docuParameters)
                {
                    src 	    = docuParameters.DefaultSharePointServer;
                    System.UriBuilder builder = new System.UriBuilder(src);
                    hostName    = builder.Host;
                   
                    externalId = xUserInfo::getCurrentUserExternalId();
                    Info (externalId);
    
                    try
                    {
                        //proxy = SharePointHelper::CreateProxy(hostName, '/', externalId);
                        proxy = SharePointHelper::CreateProxy(hostName, '/sites/XXXXXProjects', externalId);
                    }
                    catch(Exception::CLRError)
                    {
                        proxy = null;
                    }
                }
    
                if(proxy)
                {
                    if(SharePointHelper::VerifyAuthentication(proxy))
                    {
                        Microsoft.Dynamics.AX.Framework.FileManagement.SharePointDocumentStorageProvider prov = new Microsoft.Dynamics.AX.Framework.FileManagement.SharePointDocumentStorageProvider(proxy, folderPath);
    
                        prov.SaveFileWithOverwrite(newguid(), fileNameExt, fileContentType, memoryStream);
    
                        info('file uploaded');
                    }
                    else
                    {
                        info('@ApplicationFoundation:SPServerUserNotAuthorized');
                    }
       
                }
                else
                {
                    throw Error("SharePoint connection error");
                }
                ttscommit;
            }
            catch (ex)
            {
                System.Exception e = ex;
                while (e != null)
                {
                    errorMessage += e.Message;
                    e = e.InnerException;
                }
                if (appl.ttsLevel() > 0)
                {
                    ttsabort;
                }
                checkFailed("Process failed");
            }
        }
    
        Public System.IO.Stream readfromAzureBlob(DocuRef _docuRef)
        {
            AsciiStreamIo 	file;
            container 	record;
            str 		downloadUrl;
           
            if (_docuRef.isValueAttached())
            {
                System.IO.Stream docuRefStream = DocumentManagement::getAttachmentStream(_docuRef);
                return docuRefStream;
            }
    
            return null;
        }
    
    }
     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,884 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,758 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans