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, ...
Suggested Answer

Automate process for upload an excel file from sharepoint to dynamics

(0) ShareShare
ReportReport
Posted on by 53

Hi

I want to automate the process of uploading an excel file to dynamics from a particular sharepoint location.

How to achieve this ?

Thanks

  • Suggested answer
    Mohit Rampal Profile Picture
    12,573 Moderator on at

    This can be done using power automate to connect with SharePoint site and for uploading you can use F&O connector and connect with related data entity.

    May I know where in D365FO you want to upload the excel files.

  • rsingh62 Profile Picture
    53 on at

    I want to upload in Expense Management Module

  • rsingh62 Profile Picture
    53 on at

    Hi

    I want to automate the process of uploading excel from sharepoint to dynamics365 FO. This automation should trigger on a daily basis.

    I got below solution from ChatGPT but getting few errors.

    using Microsoft.Dynamics.AX.Framework.FileManagement;
    
    using Microsoft.Dynamics.AX.Framework.Services.Client;
    using Microsoft.Dynamics.AX.Framework.Services.OrganizationService;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;
    
    
    class RP_SharepointAutomate
    {
    
        static void uploadExcelToDynamics(Args _args)
        {
            // Set SharePoint and Dynamics 365 connection details
            str sharepointUrl = "https://your-sharepoint-site.sharepoint.com/sites/your-site";
                str documentLibraryName = "Documents";
            str dynamicsUrl = "https://your-dynamics-instance.api.dynamics.com";
                str tenantId = "your-tenant-id";
            str clientId = "your-client-id";
            str clientSecret = "your-client-secret";
    
            // Authenticate to SharePoint
            SharePointOnlineCredentials sharepointCredentials = new SharePointOnlineCredentials("your-username", "your-password");
            System.Net.WebClient webClient = new System.Net.WebClient();
            webClient.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
            webClient.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
            webClient.Credentials = sharepointCredentials;
            str fileUrl = sharepointUrl   "/"   documentLibraryName   "/"   "your-excel-file.xlsx";
            byte[] fileContent = webClient.DownloadData(fileUrl);
    
            // Authenticate to Dynamics 365
            System.Uri dynamicsUri = new System.Uri(dynamicsUrl);
            System.IdentityModel.Tokens.ClientCredentials deviceCredentials = new System.IdentityModel.Tokens.ClientCredentials();
            deviceCredentials.UserName.UserName = clientId;
            deviceCredentials.UserName.Password = clientSecret;
            Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext authenticationContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext("https://login.microsoftonline.com/"   tenantId);
                Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationResult authenticationResult = authenticationContext.AcquireToken(dynamicsUrl, deviceCredentials);
            str accessToken = authenticationResult.AccessToken;
    
            // Connect to Dynamics 365
            Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy organizationServiceProxy = new Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy(dynamicsUri, null, deviceCredentials, null);
            organizationServiceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
            organizationServiceProxy.HeaderToken = accessToken;
    
            // Upload the Excel file to Dynamics 365
            Microsoft.Xrm.Sdk.Entity documentEntity = new Microsoft.Xrm.Sdk.Entity("annotation");
            documentEntity.Attributes["filename"] = "your-excel-file.xlsx";
            documentEntity.Attributes["documentbody"] = System.Convert::ToBase64String(fileContent);
            documentEntity.Attributes["mimetype"] = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            System.Guid documentId = organizationServiceProxy.Create(documentEntity);
            info(strFmt("Excel file uploaded to Dynamics 365 with ID %1", documentId));
        }
    
    }
     

    Please suggest a solution for this problem. Either through enhancement in this code or by providing new solution or approach. 

    Regards

  • Anton Venter Profile Picture
    21,302 Super User 2026 Season 2 on at

    You can create a LogicApp to periodically upload the files from SharePoint to Dynamics 365 Finance.

    This post is a duplicate of community.dynamics.com/.../automate-process-for-upload-an-excel-file-from-sharepoint-to-dynamics ?

  • André Arnaud de Calavon Profile Picture
    307,889 Super User 2026 Season 2 on at

    It was obvious the same topic. I have merged the questions to prevent any confusion on the status of both of them. Now it is one question again.

    rsingh62,

    I doubt if ChatGPT is able to replace Dynamics 365 F&O developers. Can you tell what exact errors you get? When looking at the generated coding, it is using Dynamics 365 CRM references, and this is not importing data in Dynamics 365 F&O expense management.

    Like mentioned above, you can use tools like Logic App or Power Automate. If you insist re-inventing the wheel using x++ coding, you will need to find examples in the application or on the internet.

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

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

#1
Martin Dráb Profile Picture

Martin Dráb 510 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
CU-1234529-002 Profile Picture

CU-1234529-002 197

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans