upload file to sharepoint in D365FO x++ code

Hello Guys,

Here is the code snippet to upload file into sharepoint site using x++ code D365FO.

     public void uploadFiletoSharepoint()
    {
 
   //upload to sharepoint site

        str site = '/Sites/TestSharepointSite';
        str folder = 'TestDocumentLibrary/TestFolder';
        System.UriBuilder builder = new System.UriBuilder('Test.sharepoint.com');
        str host = builder.Host;
        str extId = xUserInfo::getCurrentUserExternalId();
        str filename = 'Testdata.csv';

        SharePointDocumentStorageProvider storageProvider =
            new SharePointDocumentStorageProvider(host, site, folder, extId);

        storageProvider.ProviderId = DocuStorageProviderType::SharePoint;
         
        memoryStream.Position = 0;

        boolean fileExists;
         
        fileExists = storageProvider.FileExists(filename);
        if(fileExists)
        {
         
            if(memoryStream.Length  > 0  )
            {
                storageProvider.SaveFileWithOverwrite(newGuid(), filename, 'application/csv', memoryStream);
            }
        }
        else
        {
            if(memoryStream.Length  > 0  )
            {
                storageProvider.SaveFile(newGuid(), filename, 'application/csv', memoryStream);
            }
}

    }

Hope it helps.
Thanks.
Have a great day.

Comments

  1. Hello,
    thank u for this help,
    In my case I need to read excel files from sharepoint
    any helpful resources.

    ReplyDelete

Post a Comment

Popular posts from this blog

x++ code to add hyperlink to a field in D365FO

x++ code to get Vendor Email Address based on Purpose in Dynamics 365 for Operations

x++ code to get Customer Address based on purpose in ax 2012