Sample script: quickly remove pending SharePoint API permission requests


When building SharePoint Framework solutions connected to APIs secured with Azure Active Directory, you’ll easily end up with many pending permission requests. Here’s a sample script to quickly clean them up.

#!/usr/bin/env zx
$.verbose = false;

console.log('Retrieving permission requests...');
const permissionRequests = JSON.parse(await $`m365 spo sp permissionrequest list -o json`);

for (let i = 0; i < permissionRequests.length; i++) {
  const request = permissionRequests[i];
  console.log(`Removing request ${request.Resource}/${request.Scope} (${request.Id})...`);
  try {
    await $`m365 spo sp permissionrequest deny --requestId ${request.Id}`
    console.log(chalk.green('DONE'));
  }
  catch (err) {
    console.error(err.stderr);
  }
}

This script uses CLI for Microsoft 365 and Google zx. To run the script, save it to a file with the .mjs extension. Next, run the script either by calling zx remove-permissionrequests.mjs or ./remove-permissionrequests.mjs after making the script executable using chmod +x ./remove-permissionrequests.mjs;

Using CLI for Microsoft 365, the script first retrieves the list of pending SharePoint API permission requests. Then, it iterates through the requests and removes (denies) each one of them using CLI for Microsoft 365. After running this script, your list of pending SharePoint API permission requests will be empty. Must-have in the toolbox of every Microsoft 365 developer!

Screenshot of an empty list of SharePoint API permission requests

Others found also helpful: