Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows case-insensitive column name 'Id' on spo listitem batch remove. #6427

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/m365/spo/commands/listitem/listitem-batch-remove.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,22 @@ describe(commands.LISTITEM_BATCH_REMOVE, () => {
assert(postStub.calledOnce);
});

it('removes items in batch from a SharePoint list retrieved by id when using a csv file with different casing for the ID column', async () => {
sinonUtil.restore(cli.promptForConfirmation);
sinon.stub(cli, 'promptForConfirmation').resolves(true);

sinon.stub(fs, 'readFileSync').returns(`id\n1`);
const postStub = sinon.stub(request, 'post').callsFake(async (opts: any) => {
if (opts.url === `${webUrl}/_api/$batch`) {
return mockBatchSuccessfulResponse;
}
throw 'Invalid request';
});

await command.action(logger, { options: { webUrl: webUrl, filePath: filePath, listId: listId, recycle: true, verbose: true } });
assert(postStub.calledOnce);
});

it('removes items from a sharepoint list retrieved by id when passing a list of ids via string', async () => {
const postStub = sinon.stub(request, 'post').callsFake(async (opts: any) => {
if (opts.url === `${webUrl}/_api/$batch`) {
Expand Down
21 changes: 13 additions & 8 deletions src/m365/spo/commands/listitem/listitem-batch-remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,24 @@ class SpoListItemBatchRemoveCommand extends SpoCommand {
const fileContent = fs.readFileSync(args.options.filePath, 'utf-8');
const jsonContent: any[] = formatting.parseCsvToJson(fileContent);

if (!jsonContent[0].hasOwnProperty('ID')) {
const idKey = Object.keys(jsonContent[0]).find(key => key.toLowerCase() === 'id');

if (!idKey) {
return `The file does not contain the required header row with the column name 'ID'.`;
}

const nonNumbers = jsonContent.filter(element => isNaN(Number(element['ID'].toString().trim()))).map(element => element['ID']);
const invalidIDs = validation.isValidPositiveIntegerArray(jsonContent.map(element => element[idKey].toString().trim()).join(','));

if (nonNumbers.length > 0) {
return `The specified ids '${nonNumbers.join(', ')}' are invalid numbers.`;
if (invalidIDs !== true) {
return `The file contains one or more invalid IDs: '${invalidIDs}'.`;
}
}

if (args.options.ids) {
const nonNumbers = formatting.splitAndTrim(args.options.ids).filter(element => isNaN(Number(element)));
const isValidIntegerArray = validation.isValidPositiveIntegerArray(args.options.ids);

if (nonNumbers.length > 0) {
return `The specified ids '${nonNumbers.join(', ')}' are invalid numbers.`;
if (isValidIntegerArray !== true) {
return `Option 'ids' contains one or more invalid IDs: '${isValidIntegerArray}'.`;
}
}

Expand Down Expand Up @@ -167,7 +169,10 @@ class SpoListItemBatchRemoveCommand extends SpoCommand {
if (args.options.filePath) {
const csvContent = fs.readFileSync(args.options.filePath, 'utf-8');
const jsonContent = formatting.parseCsvToJson(csvContent);
idsToRemove = jsonContent.map((item: { ID: string }) => item['ID']);

const idKey = Object.keys(jsonContent[0]).find(key => key.toLowerCase() === 'id');

idsToRemove = jsonContent.map((item: any) => item[idKey!]);
}
else {
idsToRemove = formatting.splitAndTrim(args.options.ids!);
Expand Down