Forum Discussion
Sanju405
Dec 20, 2023Copper Contributor
Unable to delete One Drive files using graph api
I am getting 403 error when trying to delete file from One Drive.
Steps I have taken:
Created App called OneDriveDeleete in Azure, with Delegated Access for
Files.ReadWrite.All. Granted Admin consent.
Created SPFX webpart with the following web api permission
"webApiPermissionRequests": [
{
"resource": "OneDriveDelete",
"scope": "Files.ReadWrite.All"
}],
This API access was also approved in Sharepoint Admin center.
This is the code,
constructor(props: IOneDriveDeleteProps) {
super(props);
this.state = {
files: []
};
this.props.context.msGraphClientFactory
.getClient("3")
.then((client: MSGraphClientV3) => {
this.graphClient = client;
});
}
const handleDeleteClick = async (fileid: string):Promise<void> => {
// Handle delete button click
try {
// Ensure the graphClient is initialized
if (!this.graphClient) {
console.error('Graph client not initialized.');
return;
}
// Make a DELETE request to the Microsoft Graph API to delete the file
const response = await this.graphClient.api(`/me/drive/items/0175NRJMNZV2ZMLXHYWJA3A7OPGDCOXKBJ`).delete();
if (response.ok) {
console.log('File deleted successfully.');
} else {
console.error('Error deleting file:', response.statusText);
}
} catch (error) {
console.error('Error deleting file:', error);
}
};
Please help
- Sanju405Copper Contributor
Is this the right forum for this query? Is this forum active? Can somebody guide me to the right forum for this query?