web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

Upload to SharePoint by code - Credentials - x++

(0) ShareShare
ReportReport
Posted on by 5

Hello everyone

I am trying to upload an .xml file (an report) to SharePoint. Before we began coding we tried to do it with Electronic report but due to all the custom fields it was not possible.

My code have been inspired by these examples:
https://allaboutdynamic.com/2020/07/06/d365-fo-ax7-sharepoint-file-upload-using-x/

   

https://d365dev.com/2018/06/07/upload-attachments-to-sharepoint-online/

// 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);

Can anyone explain what the four parameters refer to?
]
defaultSPServer = XXXXXX.sharepoint.com (I guess this is the main sharepoint site that are referred to)
spSite = sites/dummy (What is meant by site? Is it like the folder path?
spFolderPath = Shared%20Documents/Test (This is where the file should be uploaded to (Sharepoint folder path)?) 
externalId = extId (Can I replace this with my online account user account? If possible should it not be necessary to also use my password?

The AX have access to the Sharepoint (Tested in DocuRef-Sharepoint tester), as do I to the online Sharepoint.

The error I have received are error 401 (Site not found) Maybe because my site are incorrect.
But the error I am stopped at right now are:
Microsoft.Dynamics.AX.Framework.FileManagement.DocumentStorageProviderException: 'You are not authorized to access this SharePoint location.'

Can I upload by using my online credentials (Online Sharepoint User and password)?

My Share point Code:
public void sharepointUpload(System.IO.MemoryStream _memoryStream)
{

System.UriBuilder builder = new System.UriBuilder("">Dummmy.sharepoint.com/.../Test");
str host = builder.Host;
str extId = xUserInfo::getExternalId(); // Also tested by using my sharepoint user account but failed
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(host, "sites/test", "Shared%20documents/Test", extId);

/* Not used method has a stream parameter that are used from the xml build
System.Byte[] reportBytes = new System.Byte[0]();
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
reportBytes = enc.GetBytes("TEST");
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(reportBytes);
//memoryStream = comma.getStream();
*/
documentLocation = provider.SaveFile(newGuid(), "TextFile.xml", System.Web.MimeMapping::GetMimeMapping("TextFile.xml"), _memoryStream);
}


  • Martin Dráb Profile Picture
    241,315 Most Valuable Professional on at

    Which version is it about? You're asking in the AX forum but the links are about F&O.

  • Matthew Shein Profile Picture
    5 on at

    It should be F&O, D365.

  • Alex Panait Profile Picture
    170 on at

    If you are using D365 then the Docu class has a built-in method to get the storage providers. 

    However this is less then ideal you should use the ER report since you can end up with sharepoint version compatibilities later on.

    With the ER solution those will be handled as part of future platform updates.

  • nunomaia Profile Picture
    27 Moderator on at

    Yes, it is possible to export to Sharepoint online.

    If you check ER, it's exports to Sharepoint

    In a project, we have exported data to Blob Storage and enabled Power Automate to transfer data from Blog Storage to Power Automate  

  • tristansenk99 Profile Picture
    89 on at

    Did you ever come to a resolution on this? I have the same requirement.

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the August Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 507 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 419 Super User 2026 Season 2

#3
CU-1234529-002 Profile Picture

CU-1234529-002 155

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans