diff --git a/docs/docs/cmd/spo/listitem/listitem-batch-add.mdx b/docs/docs/cmd/spo/listitem/listitem-batch-add.mdx index f01cc480d81..55ee8d87d2a 100644 --- a/docs/docs/cmd/spo/listitem/listitem-batch-add.mdx +++ b/docs/docs/cmd/spo/listitem/listitem-batch-add.mdx @@ -13,12 +13,15 @@ m365 spo listitem batch add [options] ## Options ```md definition-list -`-p, --filePath ` -: The absolute or relative path to a flat file containing the list items. - `-u, --webUrl ` : URL of the site. +`-p, --filePath [filePath]` +: The absolute or relative path to a flat file containing the list items. + +`-c, --csvContent [csvContent]` +: A string content in CSV-format containing the list items. Specify filePath or csvContent, but not both. + `-l, --listId [listId]` : ID of the list. Specify either `listTitle`, `listId` or `listUrl`, but not multiple. diff --git a/src/m365/spo/commands/listitem/listitem-batch-add.spec.ts b/src/m365/spo/commands/listitem/listitem-batch-add.spec.ts index 02e6a1a59be..f70459c1bdd 100644 --- a/src/m365/spo/commands/listitem/listitem-batch-add.spec.ts +++ b/src/m365/spo/commands/listitem/listitem-batch-add.spec.ts @@ -91,6 +91,17 @@ describe(commands.LISTITEM_BATCH_ADD, () => { await command.action(logger, { options: { webUrl: webUrl, filePath: filePath, listId: listId, verbose: true } } as any); }); + it('adds items in batch to a sharepoint list retrieved by id with csv content', async () => { + sinon.stub(request, 'post').callsFake(async (opts: any) => { + if (opts.url === `${webUrl}/_api/$batch`) { + return Promise.resolve(mockBatchSuccessfulResponse); + } + throw 'Invalid request'; + }); + + await command.action(logger, { options: { webUrl: webUrl, csvContent: csvContent, listId: listId, verbose: true } } as any); + }); + it('adds items in batch to a sharepoint list retrieved by title', async () => { sinon.stub(fs, 'readFileSync').callsFake(_ => csvContent); sinon.stub(request, 'post').callsFake(async (opts: any) => { diff --git a/src/m365/spo/commands/listitem/listitem-batch-add.ts b/src/m365/spo/commands/listitem/listitem-batch-add.ts index 2caa4294160..f92ac940adb 100644 --- a/src/m365/spo/commands/listitem/listitem-batch-add.ts +++ b/src/m365/spo/commands/listitem/listitem-batch-add.ts @@ -16,8 +16,9 @@ interface CommandArgs { } interface Options extends GlobalOptions { - filePath: string; webUrl: string; + filePath?: string; + csvContent?: string; listId?: string; listTitle?: string; listUrl?: string; @@ -58,6 +59,8 @@ class SpoListItemBatchAddCommand extends SpoCommand { #initTelemetry(): void { this.telemetry.push((args: CommandArgs) => { Object.assign(this.telemetryProperties, { + filePath: typeof args.options.filePath !== 'undefined', + csvContent: typeof args.options.csvContent !== 'undefined', listId: typeof args.options.listId !== 'undefined', listTitle: typeof args.options.listTitle !== 'undefined', listUrl: typeof args.options.listUrl !== 'undefined' @@ -68,10 +71,13 @@ class SpoListItemBatchAddCommand extends SpoCommand { #initOptions(): void { this.options.unshift( { - option: '-p, --filePath ' + option: '-u, --webUrl ' }, { - option: '-u, --webUrl ' + option: '-p, --filePath [filePath]' + }, + { + option: '-c, --csvContent [csvContent]' }, { option: '-l, --listId [listId]' @@ -98,7 +104,7 @@ class SpoListItemBatchAddCommand extends SpoCommand { return `${args.options.listId} in option listId is not a valid GUID`; } - if (!fs.existsSync(args.options.filePath)) { + if (args.options.filePath && !fs.existsSync(args.options.filePath)) { return `File with path ${args.options.filePath} does not exist`; } @@ -118,16 +124,16 @@ class SpoListItemBatchAddCommand extends SpoCommand { } #initOptionSets(): void { - this.optionSets.push({ options: ['listId', 'listTitle', 'listUrl'] }); + this.optionSets.push({ options: ['listId', 'listTitle', 'listUrl'] }, { options: ['filePath', 'csvContent'] }); } public async commandAction(logger: Logger, args: CommandArgs): Promise { try { if (this.verbose) { - await logger.logToStderr(`Starting to create batch items from csv at path ${args.options.filePath}`); + logger.logToStderr(`Starting to create batch items from csv ${args.options.filePath ? `at path ${args.options.filePath}` : `from content ${args.options.csvContent}`}`); } - const csvContent = fs.readFileSync(args.options.filePath, 'utf8'); - const jsonContent = formatting.parseCsvToJson(csvContent); + const csvContent = args.options.filePath ? fs.readFileSync(args.options.filePath, 'utf8') : args.options.csvContent; + const jsonContent = formatting.parseCsvToJson(csvContent!); await this.addItemsAsBatch(jsonContent, args.options, logger); } catch (err: any) {