简体   繁体   中英

Delete locked/open file via Graph API/SDK

I'm trying to delete a file in a document library (SharePoint online) via the Graph SDK

await client
    .Drives[driveId]
    .Items[sourceItemId]
    .Request()
    .DeleteAsync();

This work. But if the file is open it'll throw this error:

Status Code: Locked
Microsoft.Graph.ServiceException: Code: resourceLocked
Message: The resource you are attempting to access is locked

How do I force it to delete anyway?

If I try to delete the open file via the SharePoint online UI (browser) then I get a popup saying it's locked but I can choose to delete it anyway. So deleting locked/open files is possible. They just forgot to document it here https://docs.microsoft.com/en-us/graph/api/driveitem-delete?view=graph-rest-1.0&tabs=csharp

I was also using graph SDK to perform the same action, but haven't found any useful thread to remove/ignore the existing file lock. Then I found one method named DeleteWithParameters() under Microsoft.SharePoint.Client.File class, which accepts one parameter of type FileDeleteParameters through which we can set BypassSharedLock parameter as true to bypass the existing lock. The code will be like the following:

 Microsoft.SharePoint.Client.FileDeleteParameters fileParams = new Microsoft.SharePoint.Client.FileDeleteParameters();
 fileParams.BypassSharedLock = true;
 var testDocument = clientContext.Web.GetFileByServerRelativeUrl(uri.LocalPath);
 testDocument.DeleteWithParameters(fileParams);
 clientContext.ExecuteQuery();

For your reference :

在此处输入图片说明

Please note: I know this is not answering the exact question, since the OP is asked for an option to remove/ignore the lock using graph SDK. But still, others can use this approach since it's a part of Microsoft.SharePoint.Client not any external sdk.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM