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

6.3 KB
Raw Blame
import { microsoftSharePointAuth } from '../auth';
import {
  createAction,
  OAuth2PropertyValue,
  Property,
  DropdownOption,
  PiecePropValueSchema,
} from '@activepieces/pieces-framework';
import { getGraphBaseUrl } from '../common/microsoft-cloud';
import { microsoftSharePointCommon } from '../common';
import { Client, PageCollection } from '@microsoft/microsoft-graph-client';
import { DriveItem } from '@microsoft/microsoft-graph-types';

export const moveFileAction = createAction({
  auth: microsoftSharePointAuth,
  name: 'microsoft_sharepoint_move_file',
  classification: 'WRITE',
  displayName: 'Move File',
  description: 'Move a file from one folder to another within the same drive.',
  audience: 'both',
  aiMetadata: {
    description: 'Moves a file to a different folder within the same SharePoint drive, optionally renaming it in the process; select the root to move to the drive top level. Use to relocate an existing file rather than copy it. Idempotent: moving a file already at the destination (with the same name) leaves it where it is.',
    idempotent: true,
  },
  props: {
    siteId: microsoftSharePointCommon.siteId,
    driveId: microsoftSharePointCommon.driveId,
    fileId: Property.Dropdown({
      auth: microsoftSharePointAuth,
      displayName: 'File to Move',
      description: 'The file you want to move.',
      required: true,
      refreshers: ['siteId', 'driveId'],
      options: async ({ auth, siteId, driveId }) => {
        if (!auth || !siteId || !driveId) {
          return {
            disabled: true,
            placeholder: 'Select a site and drive first.',
            options: [],
          };
        }
        const authValue = auth as PiecePropValueSchema<
          typeof microsoftSharePointAuth
        >;
        const cloud = (authValue as OAuth2PropertyValue).props?.['cloud'] as string | undefined;
        const client = Client.initWithMiddleware({
          authProvider: {
            getAccessToken: () => Promise.resolve(authValue.access_token),
          },
          baseUrl: getGraphBaseUrl(cloud),
        });
        const options: DropdownOption<string>[] = [];
        let response: PageCollection = await client
          .api(
            `/drives/${driveId}/root/children?$select=id,name,file`
            // `/sites/${siteId}/drives/${driveId}/root/children?$select=id,name,file`
          )
          .get();
        while (response.value.length > 0) {
          for (const item of response.value as DriveItem[]) {
            if (item.file) {
                options.push({ label: item.name!, value: item.id! });
            }
          }
          if (response['@odata.nextLink']) {
            response = await client.api(response['@odata.nextLink']).get();
          } else {
            break;
          }
        }
        return { disabled: false, options };
      },
    }),
    destinationFolderId: Property.Dropdown({
      auth: microsoftSharePointAuth,
      displayName: 'Destination Folder',
      description: 'The folder to move the file into. Select "Root" to move to the top level of the drive.',
      required: true,
      refreshers: ['siteId', 'driveId'],
      options: async ({ auth, siteId, driveId }) => {
        if (!auth || !siteId || !driveId) {
          return {
            disabled: true,
            placeholder: 'Select a site and drive first.',
            options: [],
          };
        }
        const authValue = auth as PiecePropValueSchema<
          typeof microsoftSharePointAuth
        >;
        const cloud = (authValue as OAuth2PropertyValue).props?.['cloud'] as string | undefined;
        const client = Client.initWithMiddleware({
          authProvider: {
            getAccessToken: () => Promise.resolve(authValue.access_token),
          },
          baseUrl: getGraphBaseUrl(cloud),
        });
        const options: DropdownOption<string>[] = [
          { label: 'Root', value: 'root' },
        ];
        let response: PageCollection = await client
          .api(
            // `/drives/${driveId}/root/children?$filter=folder ne null&$select=id,name`
            `/sites/${siteId}/drives/${driveId}/root/children?$filter=folder ne null&$select=id,name`
          )
          .get();
        while (response.value.length > 0) {
          for (const item of response.value as DriveItem[]) {
            options.push({ label: item.name!, value: item.id! });
          }
          if (response['@odata.nextLink']) {
            response = await client.api(response['@odata.nextLink']).get();
          } else {
            break;
          }
        }
        return { disabled: false, options };
      },
    }),
    newName: Property.ShortText({
      displayName: 'New Name (Optional)',
      description: 'Provide a new name to rename the file during the move.',
      required: false,
    }),
  },
  async run(context) {
    const { driveId, fileId, destinationFolderId, newName } =
      context.propsValue;

    const cloud = context.auth.props?.['cloud'] as string | undefined;
    const client = Client.initWithMiddleware({
      authProvider: {
        getAccessToken: () => Promise.resolve(context.auth.access_token),
      },
      baseUrl: getGraphBaseUrl(cloud),
    });

    try {
      const effectiveDestinationId =
        destinationFolderId === 'root'
          ? (await client.api(`/drives/${driveId}/root`).get()).id
          : destinationFolderId;

      const requestBody: {
        parentReference: { id?: string };
        name?: string;
      } = {
        parentReference: {
          id: effectiveDestinationId,
        },
      };

      if (newName) {
        requestBody.name = newName;
      }

      const response = await client
        .api(`/drives/${driveId}/items/${fileId}`)
        .patch(requestBody);
      
      return {
        success: true,
        message: 'File moved successfully',
        item: response,
      };
    } catch (error: any) {
      if (error.statusCode === 400) {
        throw new Error(`Invalid request: ${error.message || 'Check your input parameters'}`);
      }
      if (error.statusCode === 403) {
        throw new Error('Insufficient permissions to move this file. Requires Files.ReadWrite or higher.');
      }
      if (error.statusCode === 404) {
        throw new Error('File or destination not found. Please verify the IDs.');
      }
      throw new Error(`Failed to move file: ${error.message || 'Unknown error'}`);
    }
  },
});