You read an Excel file from SharePoint in X++ by downloading the file to a local or temporary location using the SharePoint REST API (or the .NET HttpClient class), then opening that local copy with the Excel Interop assembly or a third-party library such as ClosedXML, and finally iterating the worksheet rows. X++ itself has no built-in SharePoint connector, so the download step must be done through .NET interop or by calling a server-side helper. The steps below assume Dynamics 365 Finance and Operations (D365FO) or AX 2012 with .NET interop available; labels in the SharePoint UI can differ slightly by version and tenant configuration.
What You'll Need
- Access to the SharePoint site and library containing the Excel file, plus a valid authentication method (OAuth token, app-only credential, or a service account with delegated access).
- A development environment where you can write X++ and add .NET references (Visual Studio with the D365FO or AX 2012 X++ project).
- The full server-relative or absolute URL of the Excel file, for example
https://contoso.sharepoint.com/sites/Finance/Shared Documents/Budget.xlsx. - A library reference for reading Excel: either Microsoft Excel Interop (requires Excel installed on the AOS or client, which is not recommended on a server) or a managed library such as ClosedXML or EPPlus added as a reference.
- Permission to write to a temporary folder on the machine running the code (for example
System.IO.Path::GetTempPath()).
Step-by-Step
- Get the file URL. In SharePoint, open the library, select the Excel file, and copy the direct link. Confirm it ends in
.xlsx(or.xls) and that the account you will authenticate with can open it in the browser. - Obtain an access token. For app-only access, register an app in Microsoft Entra ID (Azure AD), grant it SharePoint site permissions, and request a token from the Microsoft identity platform token endpoint. For delegated access, use the signed-in user's token. Store the token in a variable, not in code.
- Download the file in X++. Use
System.Net.Http.HttpClientfrom .NET interop. Create the client, set theAuthorizationheader toBearer <token>, callGetAsyncwith the file URL, and read the response stream. Write the stream to a temporary file path usingSystem.IO.File::WriteAllBytesor aFileStream. If the response status is not success, throw an error with the status code so the failure is visible. - Verify the download. Check that the temporary file exists and is larger than zero bytes before continuing. Delete any stale copy from a previous run first so you do not read old data.
- Open the workbook. With ClosedXML, create a
XLWorkbookpointing at the temp file path. With Excel Interop, create anApplicationobject, setVisible = false, and open the workbook read-only. Close and release the object when done to avoid locking the file. - Read the worksheet. Select the worksheet by name or index, then loop from row 1 (or row 2 if row 1 is a header) to the last used row. Read each cell with
cell.Valueorcell.GetString()and map it to a table buffer or a temporary record. - Validate and process the data. Convert strings to the correct types (dates, amounts, enum values) with
str2Date,str2Numand similar functions, and skip or log rows that fail validation rather than aborting the whole import. - Clean up. Close the workbook, release COM objects if using Interop, and delete the temporary file in a
finallyblock so a failure does not leave files behind.
Common Mistakes
- Using Excel Interop on a server. Excel is not supported for server-side automation and can hang or crash the AOS. Prefer ClosedXML or EPPlus for batch imports.
- Hard-coding the SharePoint URL or token. Use parameters or a configuration table so the code works across environments.
- Forgetting to release COM objects, which leaves the file locked and blocks the next run.
- Reading the file directly from the SharePoint URL with a file API. SharePoint serves the file over HTTP, so you must download it first.
- Assuming row 1 is always a header. Check the sheet layout before mapping columns.
Tips
- Wrap the download and read in a
try...catchand log the HTTP status and any exception message to the infolog or a custom log table. - Use a temporary file name that includes a GUID to avoid collisions when multiple users run the import at once.
- Read only the used range rather than the whole sheet, which is much faster on large files.
- Test with a small sample file first, then a full-size file, before scheduling the job.
Troubleshooting
If the download returns 401 or 403, the token is expired or lacks SharePoint permissions; request a new token and confirm the app registration has the correct site-level permission. A 404 usually means the URL is wrong or the file was moved; open the link in a browser to confirm. If the workbook opens but cells are empty, check that you are reading the correct worksheet and that the used range includes your data. If the file is locked on the next run, an earlier process did not release its handle; close the workbook and release COM objects, or restart the AOS in a test environment. If X++ cannot resolve the .NET types, confirm the references are added to the project and the assembly is deployed to the bin folder.
Frequently Asked Questions
Can X++ read a SharePoint file directly without downloading it?
No. X++ has no native SharePoint file API. You must download the file over HTTP first, then read the local copy.
Do I need Excel installed to read an .xlsx file?
Not if you use a managed library such as ClosedXML or EPPlus. Excel Interop does require Excel, which is why it is not recommended on a server.
Which authentication should I use for a scheduled job?
App-only authentication with a registered Entra ID app and site permissions, because no user is signed in when the batch runs.
Can I read the file from a document library with spaces in the name?
Yes. URL-encode the path, or use the SharePoint REST endpoint that returns the file by its server-relative path.
Why does the import work in one environment but fail in another?
Usually a permissions or configuration difference. Check the app registration, the site URL, and whether the service account can access the library in each environment.