Skip to main content

Notifications

Announcements

No record found.

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

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

FILE CREATION:

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

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

FOLDER CREATION:

  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:

    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;
  3. Access token should be passed to the following C# method:

    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:

    http://www.herlitz.nu/2012/12/30/sharepoint-2013-tokenhelper-cs-source-code/

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

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

  • 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).

The post SharePoint Online Integration with Dynamics 365 for Operation (D365FO) Upload File / Create Folder appeared first on Merit Solutions.


This was originally posted here.

Comments

*This post is locked for comments