Saturday, March 19, 2022

Uploading file on SharePoint D365 FO

The following class will create a "TextFile.txt" in "MyFolder" folder of the sharepoint.

Class GITS_Testing

{

    public static void main(Args _args)

    {

        System.UriBuilder builder = new System.UriBuilder("https://osoolbh.sharepoint.com/sites/Testing");

        str extId = xUserInfo::getExternalId();

        Microsoft.Dynamics.AX.Framework.FileManagement.SharePointDocumentStorageProvider provider;

        Microsoft.Dynamics.AX.Framework.FileManagement.DocumentLocation documentLocation = new Microsoft.Dynamics.AX.Framework.FileManagement.DocumentLocation();

        provider = new Microsoft.Dynamics.AX.Framework.FileManagement.SharePointDocumentStorageProvider("osoolbh.sharepoint.com", "sites/Testing", "Shared%20Documents/MyFolder", extId);

        System.Byte[] reportBytes = new System.Byte[0]();

        System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();

        reportBytes = enc.GetBytes("AY");

        System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(reportBytes);

        documentLocation = provider.SaveFile(newGuid(), 'TextFile.txt', System.Web.MimeMapping::GetMimeMapping('TextFile.txt'), memoryStream);

    }


}