SharePoint Online Integration with Dynamics 365 for Operation (D365FO) Upload File / Create Folder

Share on linkedin
Share on twitter
Share on facebook

This blog will explain how to create files and folders on SharePoint Online programmatically, using AX7 code (Dynamics 365 for Operations).


  1. Library to use: Microsoft.Dynamics.AX.Framework.FileManagement
  2. To save file, SharePointDocumentStorageProvider class should be used:

    [crayon lang=”x++”]System.UriBuilder builder = new System.UriBuilder(SITE);
    str host = builder.Host;
    str extId = xUserInfo::getExternalId();

    SharePointDocumentStorageProvider storageProvider =
    new SharePointDocumentStorageProvider(host, SITE, FOLDER_PATH, extId);

    //Create MemoryStream object that will hold data to upload
    System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
    //Populate memoryStream with your file’s contents.
    //In the end set the position of the stream to 0
    memoryStream.Position = 0;

    //Method SaveFile is used to create the file on SharePoint online
    storageProvider.SaveFile(newGuid(), NAME_OF_FILE, MIME_TYPE, memoryStream);


  1. Library to use (D365FO): Microsoft.Dynamics.Platform.Integration.SharePoint
  2. To create folders on SharePoint, we need access token. It can be obtained in the following way:

    [crayon lang=”x++”]System.UriBuilder builder = new System.UriBuilder(SITE_ADDRESS);
    str host = builder.Host;
    str extId = xUserInfo::getExternalId();
    ISharePointProxy proxy = SharePointHelper::CreateProxy(host, ‘/’, extId);
    str token = proxy.AccessToken;[/crayon]

  3. Access token should be passed to the following C# method:

    [crayon lang=”csharp”]public static ClientContext GetClientContextWithAccessToken(string targetUrl, string accessToken)
    Uri targetUri = new Uri(targetUrl);

    ClientContext clientContext = new ClientContext(targetUrl);

    clientContext.AuthenticationMode = ClientAuthenticationMode.Anonymous;
    clientContext.FormDigestHandlingEnabled = false;
    clientContext.ExecutingWebRequest +=
    delegate (object oSender, WebRequestEventArgs webRequestEventArgs)
    webRequestEventArgs.WebRequestExecutor.RequestHeaders[“Authorization”] =
    “Bearer ” + accessToken;

    return clientContext;

    Method is taken from:

    We don’t need the whole class, just this method.

  4. Library to use (C#): Microsoft.SharePoint.Client

  5. After getting the ClientContext object from GetClientContextWithAccessToken method, we can use it to create folders (purely C# code from now on, not explained in this blog).

You Might Also Like:

This Post Has 5 Comments

  1. Hi,

    I have tried the file creation. But getting "value does not fall within the expected range" error at "storageProvider.SaveFile(newGuid(), NAME_OF_FILE, MIME_TYPE, memoryStream);".

    Below is the code:

         // Sharepoint
                 str site =
                 System.UriBuilder builder = new System.UriBuilder(site);
                 str host = builder.Host;
                 str extId = xUserInfo::getExternalId();
                 Microsoft.Dynamics.AX.Framework.FileManagement.SharePointDocumentStorageProvider storageProvider
                 = new Microsoft.Dynamics.AX.Framework.FileManagement.SharePointDocumentStorageProvider(host, "/personal/
                                 OurUserName_OurO365DomainHere_onmicrosoft_com/_api/web/", "GetFolderByServerRelativeUrl('personal/
                                 OurUserName_OurO365DomainHere_onmicrosoft_com/Documents/Shared')", extId);
                 //Populate memoryStream with your file’s contents.
                 //In the end set the position of the stream to 0
                 stream.Position = 0;
                 //Method SaveFile is used to create the file on SharePoint online
                 storageProvider.SaveFile(newGuid(), "sample.txt", "text/plain", stream);


    Could you please let me know, how to resolve this?




    1. Hello Shankar,

      I believe the problem is in the SITE and FOLDER_PATH variables that you are passing to the SharePointDocumentStorageProvider constructor. For example, if the full path of your new file is supposed to look something like

      then the HOST should be “” and SITE should be “/sites/someSite” and the FOLDER_PATH should be “someLibrary/someFolder”.

      Also, make sure that the MemoryStream object is initialized and it’s position set to 0.

      Please let me know if you still have problems.

  2. Hello All,

    What's the better way to retrieve files from Sharepoint online ? I cannot get any solution on this.

  3. Dear sirs,

    Can you give me a hint of how to GET files from Sharepoint folders? I can hardly find any information concerning this question.

  4. Hello, thank you for sharing. I am triying to create file under my sharepoint site, followed the comments above and I get this error “you are not authorized to access this sharepoint location”
    any hints ? thank you very much.

Leave a Reply