none
Remove a subfolder in a document library if a file is opened in the desktop client RRS feed

  • Question

  • There is a document library with the following structure;

    - <Doc lib root folder>

     ----- <Folder 1>

           -------<SubFolder 1.1>

                   ----------<File doc1.docx>

    I open the file in the desktop MS Word client and type a couple of characters without saving the file.

    Now I run the CSOM code that removes the folder <SubFolder 1.1> via Folder.Recycle() method and receive the exception:

    Microsoft.SharePoint.Client.ServerException: Cannot remove file "Doc 1.docx". Error Code: 6009.
       at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream)
       at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse()
       at Microsoft.SharePoint.Client.ClientContextExtensions.ExecuteQueryImplementation(ClientRuntimeContext clientContext, Int32 retryCount, Int32 delay)

    But the business requirement in this case is to remove the folder no matter what (the site is being archived, the folder is considered not important and in order to save the space in the archive it should be removed). Users are notified beforehand about the scheduled removal.

    Is there a way to force the removal of the folder (the file) in this case?

    Thursday, March 30, 2017 11:13 AM

Answers

  • Hi Vadim,

    Yes, this method is not available in CSOM now, I would suggest you use SSOM for this task currently.

    If you are going to remove to the whole folder, in case of any user would edit a document in this period, you can add a logic in your code to check and wait until a document lock is timeout, then delete it.

    Best regards,

    Patrick 

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by Vadim Maximov Tuesday, April 4, 2017 11:24 AM
    Friday, March 31, 2017 10:43 AM
    Moderator

All replies

  • $web = get-spweb "http://myfarm/mysite/mysubsite"
    $list
    = $web.Lists["My Library"]
    $item
    = $list.Items.GetItemById(123)
    $item
    .File.ReleaseLock($item.File.LockId)
    $web
    .Dispose()

    Replace the placeholders with values appropriate to your environment/list/document; use the actual subsite URL in the get-spweb cmdlet, use the library's title to retrieve it from the $web.Lists collection, and use the actual ID of the problem document as the parameter in GetItemById().

    Thursday, March 30, 2017 11:55 AM
  • Thanks! And can I get all the files with the locks via REST or CAML query? These ones seem not working:

    https://SITE/_api/web/lists/getByTitle('Shared%20Documents')/Items?$select=File/LockedByUser/Id&$expand=File/LockedByUser/Id&$filter=File/LockedByUser ne null
    
    https://SITE/_api/web/lists/getByTitle('Shared%20Documents')/Items?$select=File/LockedByUser/Id&$expand=File/LockedByUser/Id&$filter=File/LockedByUser/Id ne null


    Please, mark as answer, if this helps.

    Thursday, March 30, 2017 12:48 PM
  • Another problem is that the solution you proposed uses SSOM, not CSOM. I couldn't find ReleaseLock() method for client-side File class.

    Please, mark as answer, if this helps.

    Thursday, March 30, 2017 2:10 PM
  • Hi Vadim,

    Yes, this method is not available in CSOM now, I would suggest you use SSOM for this task currently.

    If you are going to remove to the whole folder, in case of any user would edit a document in this period, you can add a logic in your code to check and wait until a document lock is timeout, then delete it.

    Best regards,

    Patrick 

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by Vadim Maximov Tuesday, April 4, 2017 11:24 AM
    Friday, March 31, 2017 10:43 AM
    Moderator
  • Thank you, Patrick. And what about the REST query to get the locked files? What is wrong with this one:

    https://SITE/_api/web/lists/getByTitle('Shared%20Documents')/Items?$select=File/LockedByUser/Id&$expand=File/LockedByUser/Id&$filter=File/LockedByUser ne null
    It does not return values (HTTP error 400, bad request).


    Please, mark as answer, if this helps.

    Friday, March 31, 2017 11:11 AM
  • Hi Vadim,

    To access the “LockedByUser” property, it is required to access each file object individually like this:

    "/_api/web/lists/getbytitle(‘library’)/Items(3)/File/LockedByUser"

    Best regards,

    Patrick

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, April 4, 2017 11:06 AM
    Moderator