upload file to sharepoint in D365FO x++ code
Sun Sep 01 2024 09:29:05 GMT+0000 (Coordinated Universal Time)
Saved by @Manjunath
-------------------------------blog1------------------------------------------------------------- //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); } } } -----------------------------------------blog 2---------------------------------------------------- //Now, let’s determine the specific SharePoint paths by splitting up the address. That would be… //SharePoint server: d365dev.sharepoint.com //SharePoint site: sites/Test-Site //SharePoint folder path: Shared Documents/Invoices //One important thing to note: to use the D365fFO framework, it requires a D365fFO user who has a //so-called “ExternalId”. //The function could then look like this: protected void uploadToSharepoint(DocuRef _docuRef) { str filename; str fileContetType; str externalId; System.IO.Stream fileStream; DocuValue docuValue = _docuRef.docuValue(); const str defaultSPServer = 'd365dev.sharepoint.com'; const str spSite = 'sites/Test-Site'; const str spFolderPath = 'Shared Documents/Invoices'; filename = docuValue.filename(); fileContetType = System.Web.MimeMapping::GetMimeMapping(filename); // Get the file stream of the document attachment. fileStream = DocumentManagement::getAttachmentStream(_docuRef); // Specify a user who has an External Id. externalId = xUserInfo::getExternalId(curUserId()); // Instantiate SharePoint document storage provider with sharepoint address path. Microsoft.Dynamics.AX.Framework.FileManagement.IDocumentStorageProvider storageProvider = new Microsoft.Dynamics.AX.Framework.FileManagement.SharePointDocumentStorageProvider( defaultSPServer, spSite, spFolderPath, externalId); storageProvider.ProviderId = DocuStorageProviderType::SharePoint; if (storageProvider != null) { // Generates a unique file name in case the file name already exists on SharePoint path. str uniqueFilename = storageProvider.GenerateUniqueName(filename); // Upload file to SharePoint Online path. Microsoft.Dynamics.AX.Framework.FileManagement.DocumentLocation location = storageProvider.SaveFile( docuValue.FileId, uniqueFilename, fileContetType, fileStream); if (location != null) { info(strFmt("File path: %1", location.get_NavigationUri().ToString())); } } }
Comments