Skip to main content

Notifications

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

  • André Arnaud de Calavon Profile Picture
    André Arnaud de Cal... 291,969 Super User 2025 Season 1 on at
    RE: Automate process for upload an excel file from sharepoint to dynamics

    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.

  • Anton Venter Profile Picture
    Anton Venter 19,493 Super User 2025 Season 1 on at
    RE: Automate the process of uploading Excel file from sharepoint to dynamics365 FO

    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 ?

  • rsingh62 Profile Picture
    rsingh62 53 on at
    Automate the process of uploading Excel file from sharepoint to dynamics365 FO

    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

  • rsingh62 Profile Picture
    rsingh62 53 on at
    RE: Automate process for upload an excel file from sharepoint to dynamics

    I want to upload in Expense Management Module

  • Suggested answer
    Mohit Rampal Profile Picture
    Mohit Rampal 12,554 Super User 2024 Season 1 on at
    RE: Automate process for upload an excel file from sharepoint to dynamics

    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.

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,969 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,842 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans