Sunday, April 2, 2023

Upload file to share Point using x++

 Upload a file to share Point using x++.


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 DocumentsUploadToSharePoint// extends RunBaseBatch
{
    str docfiletype;
    Microsoft.Dynamics.AX.Framework.FileManagement.IDocumentStorageProvider storageProvider;
	
    public static void main(Args    args)
    {
        DocumentsUploadToSharePoint documentsUploadToSharePoint = new DocumentsUploadToSharePoint();

        documentsUploadToSharePoint.upload();
    }

    public void upload()
    {
        System.Exception            ex;
        System.IO.Stream            memoryStream;
        Filename 		    fileNameExt; 
	VendTable 		    vendTable;
	DocuRef                	    docuRef;
	DocuValue              	    docuValue;
        str 			    errorMessage;
		
	select * from docuValue
	    join docuref
	        where docuValue.RECID == docuref.VALUERECID
	    join vendTable
		where docuref.REFRECID == vendTable.RECID
		    && vendTable.accountNum =='00000112'
		    && Docuref.REFCOMPANYID == vendTable.DataAreaId;
    
        memoryStream = this.readfromAzureBlob(docuref);
        memoryStream.Seek(0, System.IO.SeekOrigin::Begin);

        fileNameExt =  docuValue.FileName + '.' + docuValue.FileType;
	//str folderPath = "/sites/'new site'/'Folder'";
	str folderPath = "/MyFolder";
	str fileContentType = System.Web.MimeMapping::GetMimeMapping(fileNameExt);

	ISharePointProxy    proxy = null;
	DOCUPARAMETERS      docuParameters;
	str                 src = '';
	str                 hostName = '';

	docuParameters = DOCUPARAMETERS::find();
 
        try
        {
            ttsbegin;
	    if(docuParameters)
	    {
	        src 	    = docuParameters.DefaultSharePointServer;
	        hostName    = builder.Host;  
			
	        System.UriBuilder builder = new System.UriBuilder(src);

	        str externalId = xUserInfo::getCurrentUserExternalId();

	        try
	        {
		    //proxy = SharePointHelper::CreateProxy(hostName, '/', externalId);
		    proxy = SharePointHelper::CreateProxy(hostName, '/sites/MySite', 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);
	    }
	    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");
	error(errorMessage);
    }

    Public System.IO.Stream readfromAzureBlob(DocuRef _docuRef)
    {
        AsciiStreamIo 	file;
        container 	record;
        str 		downloadUrl;
       
	//if (docuValue.FileType != 'PDF')
	//{
	//    continue;
	//}
		
	if (_docuRef.isValueAttached())
        {
	    System.IO.Stream docuRefStream = DocumentManagement::getAttachmentStream(_docuRef);
	    return docuRefStream;
	}
		
        /*if (_docuRef.isValueAttached())
        {
            var docuValueloc = _docuRef.docuValue();
            //downloadUrl = docuValueloc.Path;

            if (!downloadUrl || docuValueloc.Type == DocuValueType::Others)
            {
                str accessToken = DocumentManagement::createAccessToken(_docuRef);
                downloadUrl = Microsoft.Dynamics.AX.Framework.FileManagement.URLBuilderUtilities::GetDownloadUrl(docuValueloc.FileId, accessToken);
            }
			
            //storageProvider = new Microsoft.Dynamics.AX.Framework.FileManagement.IDocumentStorageProvider();
            var  docContents = storageProvider.GetFile(docuValueloc.createLocation());
            //file = AsciiStreamIo::constructForRead(docContents.Content);//File::UseFileFromURL(downloadUrl));
            return docContents.Content;
        }*/
        return null;
    }

}


Keep Daxing!!

No comments:

Post a Comment