Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Unanswered

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

Posted on by 742
thread removed
  • Upload PDF file to sharepoint using X++ code in D365 F&O
    Where the content? tiny fishing
  • Rhushikesh R Profile Picture
    Rhushikesh R 742 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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans