Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if file locked/open via Graph API/SDK

I'm trying to delete a file in a document library (personal OneDrive) via the Graph SDK (using c#)

await graphClient.Drives[driveId].Items[driveItemId].Request().DeleteAsync();

It works. But if the file is open it'll throw this error:

423 : Locked
....
"error": {
    "code": "resourceLocked",
    "message": "The resource you are attempting to access is locked",
    "innerError": {
      "request-id": "d1bfa1f2-cXXXXX",
      "date": "2020-05-02T04:05:23"
    }

Is there any possibility to check if file is locked/open via Graph API/SDK?)

My current solution is: i make this call

await graphClient.Drives["{driveid}"].Items["{driveItemid}"].Checkout().Request().PostAsync();

In case if file is open i ge t the same 423 error response. Then i ask user to make it close and repeat. In case if i get 204 No content response i make call to delete driveItem.

But i do not think it is an elegant problem solving. Thank you

like image 557
Maryna Shubna Avatar asked Oct 17 '25 08:10

Maryna Shubna


1 Answers

The solution that we found was making and passing a HeaderOption that bypasses the shared lock, then adding it into the request. This bypasses the locked file 423 error response. And allows the graphServiceClient to delete the file regardless of whether it is open or not.

List<Microsoft.Graph.Option> reqOptions = new List<Microsoft.Graph.Option>
    {
        new Microsoft.Graph.HeaderOption("Prefer", "bypass-shared-lock")
    };

await graphServiceClient.Groups[groupId].Drive.Items[driveItem.Id].Request(reqOptions).DeleteAsync();
like image 95
jjirinec Avatar answered Oct 21 '25 05:10

jjirinec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!