web
You’re offline. This is a read only version of the page.
close
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 758
thread removed
I have the same question (0)
  • Rhushikesh R Profile Picture
    758 on at
    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;
        }
    
    }
     
  • Community member Profile Picture
    6 on at
    Where the content? tiny fishing

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 449 Super User 2025 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 422 Most Valuable Professional

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 239 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans