How can I get the path of deleted folder on Sharepoint Online by CSOM ?

Sung-Uk Park 1 Reputation point
2022-01-21T03:13:03.367+00:00

We are developing an application that crawl files from SPO using CSOM for .NET Core.
The application executes two kind of crawls:

  1. Crawl the entire Document Library by getting each of the files in it. Then create an index of all the files
  2. We are using ChangeLog for detect changes in the previuosly crawled Document Library
    Using ChangeQuery we filter the files by ChangeType that have changed from the previous crawl
    Target ChangeType:
    - Add: new files
    - Rename: renamed files
    - Move: files moved to other Document Library
    - Restore: files restored from Recycle Bin
    - Delete Object: deleted files

[Problem]
If a folder that contains files is deleted from the Share Point Online, we cannot retrieve the change log of the deleted files under that folder.
We can retrieve the deleted object from change log only for the deleted folder.
We cannot get the deleted object for the files under the deleted folder from change log.

Also, we cannot identify if the deleted object is a folder or a file (FileSystemObjectType is always Invalid).
And we also cannot identify the path of deleted object from change log.

So, our work around is as below.

  1. If a deleted object is retrieved from change log, first, we always assume it is a file. And we try to delete our index of the file by SPO's uniqueId.
  2. If the uniqueId does not match any file from from our index of files, we assume it is a folder and try to get its information (Path, foler name...) from the Recycle Bin by filtering the delete date that match with change log delete date
  3. If we get a match, we try to delete the files from our index that match the deleted folder path.

But the above work around has also a problem.

  • When we retrive the recycle bin by CSOM, we can retrieve only the objects deleted by the user account (System Account or AAD Application) used to logging.
  • Although if we access to the SPO portal using the same account we can see the files that other users have deleted

[Question]

  1. Is there any other way to get the path of deleted folder from change log ?
  2. Is there any way to retrieve all recycle bin data by CSOM ?
  3. Why cannot retrieve the change logs of the files under the deleted folder ?
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,621 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,668 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. RaytheonXie_MSFT 31,071 Reputation points Microsoft Vendor
    2022-01-21T09:29:04.963+00:00

    Hi @Anonymous ,
    You can get recycle bin data by following code

        string userName = "xxx@xxx.onmicrosoft.com";    
        
        Console.WriteLine("Enter your password.");    
        SecureString password = GetPassword();    
        
        // ClienContext - Get the context for the SharePoint Online Site    
        
        using (var clientContext = new ClientContext("https://xxx.sharepoint.com"))    
        {    
            // SharePoint Online Credentials    
            clientContext.Credentials = new SharePointOnlineCredentials(userName, password);    
        
            // Get the SharePoint web    
            Web web = clientContext.Web;    
        
            // Get all the recycle bin items    
            // Reference: https://msdn.microsoft.com/EN-US/library/microsoft.sharepoint.client.recyclebinitem_members.aspx                   
            RecycleBinItemCollection rbiColl = web.RecycleBin;    
            clientContext.Load(rbiColl);    
        
            // Execute the query to the server    
            clientContext.ExecuteQuery();    
        
            // Loop through each recycle bin item    
            foreach (RecycleBinItem rbiItem in rbiColl)    
            {    
                Console.WriteLine(rbiItem.Title);    
            }    
            Console.ReadLine();    
        }    
    }    
       
    private static SecureString GetPassword()    
    {    
        ConsoleKeyInfo info;    
        
        //Get the user's password as a SecureString    
        SecureString securePassword = new SecureString();    
        do    
        {    
            info = Console.ReadKey(true);    
            if (info.Key != ConsoleKey.Enter)    
            {    
                securePassword.AppendChar(info.KeyChar);    
            }    
        }    
        while (info.Key != ConsoleKey.Enter);    
        return securePassword;    
    

    The folder is monitored as an item, so we are unable to retrieve the change logs of the files under the deleted folder. Here is the document for more details
    https://github.com/pnp/PnP/tree/master/Samples/Core.ListItemChangeMonitor


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



  2. Sung-Uk Park 1 Reputation point
    2022-01-26T06:55:56.23+00:00

    Hi @RaytheonXie_MSFT
    Thank you for your reply.
    I already read the web page which is suggested from you.
    And I didn't understand which specific permission should need.
    What is edit permission ?(to what ?)

    If you're using SharePoint in Microsoft 365, you can even view and restore items that were deleted by someone else, as long as you have edit permissions.

    I searched some other Q&A and found below link.
    https://powerusers.microsoft.com/t5/Building-Flows/GET-HTTP-Request-to-SharePoint-Results-Only-Visible-When/m-p/882442#M124031

    I thought I should use ClientContext.Site.RecycleBin instead of ClientContext.Web.RecycleBin.
    I tried get deleted data by somone else from ClientContext.Site.RecycleBin.
    And I learned that should have as below permission to use ClientContext.Site.RecycleBin.
    If CSOM using an Azure AD Service Principle to connect to SPO, the AAD Application should have "Sites.FullControl.All" permission else if CSOM using user account and password to connect, the user account should have Site Collection Admin role.

    Now I can get all deleted data (even if that is deleted by someone else) from RecycleBin by CSOM.


  3. Sung-Uk Park 1 Reputation point
    2022-01-26T06:56:34.907+00:00

    Hi @RaytheonXie_MSFT
    Thank you for your reply.
    I already read the web page which is suggested from you.
    And I didn't understand which specific permission should need.
    What is edit permission ?(to what ?)

    If you're using SharePoint in Microsoft 365, you can even view and restore items that were deleted by someone else, as long as you have edit permissions.

    I searched some other Q&A and found below link.
    https://powerusers.microsoft.com/t5/Building-Flows/GET-HTTP-Request-to-SharePoint-Results-Only-Visible-When/m-p/882442#M124031

    I thought I should use ClientContext.Site.RecycleBin instead of ClientContext.Web.RecycleBin.
    I tried get deleted data by somone else from ClientContext.Site.RecycleBin.
    And I learned that should have as below permission to use ClientContext.Site.RecycleBin.
    If CSOM using an Azure AD Service Principle to connect to SPO, the AAD Application should have "Sites.FullControl.All" permission else if CSOM using user account and password to connect, the user account should have Site Collection Admin role.

    Now I can get all deleted data (even if that is deleted by someone else) from RecycleBin by CSOM.

    0 comments No comments