Forum Discussion

Toby Statham's avatar
Toby Statham
Brass Contributor
Jul 13, 2020

Changes returned when folder deleted using ChangeQuery and GetChanges in CSOM

Have a folder in a SharePoint Document Library that has 3 files in it.

 

When I delete the folder, I'm getting one change returned in the GetChanges function. It is a ChangeItem related to the deleted folder, but I don't get any changes returned for the files in the folder.

 

Is this correct?

I'm sure in the past I used to get the files that have been deleted too, but unable to confirm.

 

This is the code I'm using currently.

 

Have tried at different scopes to (i.e. changes at Library, Root Folder etc.)

 

using (var authenticationManager = new AuthenticationManager())
using (var context = authenticationManager.GetContext(siteUri))
{
                var site = context.Site;
                context.Load(site);
                var query = new ChangeQuery(true, true)
                {
                    File = true,
                    Item = true,
                    Folder = true,
                    RecursiveAll = true,
                    DeleteObject = true,
                    Rename = false,
                    Add = false,
                    Update = false,
                    Site = false,
                    Web = false,
                    List = false,
                    User = false,
                    View = false,
                    SystemUpdate = false,
                };
                
                var changes = site.GetChanges(query);
                context.Load(changes);
                await context.ExecuteQueryAsync();

                foreach (Change change in changes)
                {
                    if (change is Microsoft.SharePoint.Client.ChangeItem)
                    {
                        ChangeItem ci = change as ChangeItem;
                        Console.WriteLine($"Item {ci.ItemId.ToString()}: {ci.ChangeType.ToString()}");
                    }

                    if (change is Microsoft.SharePoint.Client.ChangeFolder)
                    {
                        ChangeFolder cf = change as ChangeFolder;
                        Console.WriteLine($"Folder {cf.UniqueId.ToString()}: {cf.ChangeType.ToString()}");
                    }

                    if (change is Microsoft.SharePoint.Client.ChangeFile)
                    {
                        ChangeFile cf = change as ChangeFile;
                        Console.WriteLine($"Folder {cf.UniqueId.ToString()}: {cf.ChangeType.ToString()}");
                    }
                }
            }

 

2 Replies

  • Joao Livio's avatar
    Joao Livio
    Iron Contributor

    Hi Toby Statham 

     

    See this link,  I don't have any credit on the code

    https://gist.github.com/sksnips/0de89669aa5a4415a484

  • Kannibala's avatar
    Kannibala
    Copper Contributor

    Toby Statham 

     

    Hi 

     

    How to get the filename of deleted file in sharepoint.

     

    if (change is ChangeItem)
    {
    ListItem modifiedItem = changeList.GetItemById((change as ChangeItem).ItemId);
    cxt.Load(modifiedItem);
    cxt.ExecuteQuery();

    }

     

    This fails when the the file is deleted

Resources