Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

1.9 KB
Raw Blame
import { createAction } from '@activepieces/pieces-framework';
import { getCloudProp, getGraphBaseUrl } from '../common/microsoft-cloud';
import { oneDriveAuth } from '../auth';
import { oneDriveCommon } from '../common/common';
import { listFilesOutputSchema } from '../output-schemas';
import { Client, PageCollection } from '@microsoft/microsoft-graph-client';
import { DriveItem } from '@microsoft/microsoft-graph-types';

export const listFiles = createAction({
	auth: oneDriveAuth,
	name: 'list_files',
	classification: 'SEARCH',
	description: 'List files in a OneDrive folder',
	audience: 'human',
	aiMetadata: { description: 'List the files contained in a Microsoft OneDrive folder, returning only file items (subfolders are excluded) and paging through all results. Provide a parent folder ID to scope the listing, or leave it empty to list from the drive root. Read-only and idempotent.', idempotent: true },
	displayName: 'List Files',
	outputSchema: listFilesOutputSchema,
	props: {
		markdown:oneDriveCommon.parentFolderInfo,
		parentFolder: oneDriveCommon.parentFolder,
	},
	async run(context) {
		const endpoint = context.propsValue.parentFolder
			? `/me/drive/items/${context.propsValue.parentFolder}/children`
			: `/me/drive/items/root/children`;

		const files = [];

		const cloud = getCloudProp(context.auth);
		const client = Client.initWithMiddleware({
			authProvider: {
				getAccessToken: () => Promise.resolve(context.auth.access_token),
			},
			baseUrl: getGraphBaseUrl(cloud),
		});
		let response: PageCollection = await client.api(endpoint).get();

		while (response.value.length > 0) {
			const items: DriveItem[] = response.value;
			for (const item of items) {
				if (item.file) {
					files.push(item);
				}
			}

			if (response['@odata.nextLink']) {
				response = await client.api(response['@odata.nextLink']).get();
			} else {
				break;
			}
		}

		return files;
	},
});