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