Based on this old thread, you can opt for Power Automate where you set the option 'Option Include Nested items Yes.
Also, my idea is to create a recursive function to iterate through folders and subfolders (ADF pipeline with Azure Function or Logic Apps for recursion)
The Microsoft Graph API provides the capability to list items within a drive, and you can leverage the children
endpoint to recursively fetch all items.
You can set up an AAD app and grant it the necessary permissions to access SharePoint files.
And then to build the initial API Call, you can use the driveItem/children
endpoint to get the contents of a folderhe driveItem/children
endpoint to get the contents of a folder.
Just keep in mind that if you are starting from the root, {item-id}
can be replaced with root
and if you are starting from the root, {item-id}
can be replaced with root
.
https://graph.microsoft.com/v1.0/drives/{drive-id}/items/{item-id}/children
And don't forget to follow the @odata.nextLink
to handle pagination if there are more items than can be returned in a single API call.
Now comes the part of the recursivity, for each item returned by the API:
- If the item is a folder, recursively call the API to get its children.
- If the item is a file, add it to the list of files to be copied.
How to Implement it in ADF?
- Web Activity: To call the Microsoft Graph API.
- ForEach Activity: To iterate over each item in the response.
- If Condition Activity: To check if the item is a folder.
- Azure Function or Logic App: To handle recursion if the item is a folder.
- Sample Pseudo-Code for Azure Function (Python):
import requests def fetch_files_from_folder(drive_id, folder_id, access_token): url = f"https://graph.microsoft.com/v1.0/drives/{drive_id}/items/{folder_id}/children" headers = { "Authorization": f"Bearer {access_token}" } files = [] while url: response = requests.get(url, headers=headers).json() for item in response.get('value', []): if item['folder']: files.extend(fetch_files_from_folder(drive_id, item['id'], access_token)) else: files.append(item) url = response.get('@odata.nextLink', None) return files
- Integrate with ADF Pipeline:
- Activity 1: Get Access Token
- Use a Web Activity to get the OAuth2 access token.
- Activity 2: Fetch Root Folder Items
- Use another Web Activity to call the Microsoft Graph API for the root folder.
- Activity 3: ForEach Activity
- Iterate over the items in the response.
- Activity 4: Check Item Type
- Use an If Condition Activity to check if the item is a folder.
- Activity 5: Recursion
- If the item is a folder, call the Azure Function to handle recursion.
- Activity 1: Get Access Token
- Get Drive Root Items:
https://graph.microsoft.com/v1.0/drives/{drive-id}/root/children
- Get Folder Children:
https://graph.microsoft.com/v1.0/drives/{drive-id}/items/{folder-id}/children
Here are more links to help you :