检查父文件夹是否包含多个文档,然后从SharePoint 2013 CSOM中的该文件夹中删除特定文档

问题描述:

我想在CSOM中删除名为"Test"的文档。此文档存在于子文件夹中。而代码只有在其父
文件夹(即子文件夹)包含多个文档时才应删除此文档。我知道我可以使用CAML查询来获取所有带有"测试"名称的文档,然后迭代它以检查其父文件夹是否包含多个文档,然后删除此文档。但是我不能使用这种方法,因为
将对SharePoint进行更多调用(5000次调用5000个listItems以获取parentFolder文件计数)。如果你们有任何其他想法,请告诉我。 SharePoint版本是SharePoint 2013 onpremise。



Vishal

I want to delete document with name 'Test' in CSOM. this document is present in subfolder. whereas code should delete this document only if its parent folder(i.e subfolder) has more than 1 document. I know I can use CAML Query to get all documents with 'Test' name and then iterate it to check if its parent folder has more than 1 document and then delete this document. But I can not use this method as it will make more calls to SharePoint(5000 calls for 5000 listItems to get parentFolder files count). Please let me know if you guys have any other idea. SharePoint version is SharePoint 2013 onpremise.


Vishal

 

Hi, 

您可以通过Server Relative Url获取特定文件夹,然后循环文件并检查文件名,如果名称等于"Test",则然后删除它,这是一个代码片段供您参考:

You can get the specific folder by Server Relative Url and then loop the files and check the file name, if the name equals "Test" then delete it, here is a code snippet for your reference:

            ClientContext ctx = new ClientContext("http://sp/sites/dev");
            Folder Parentfolder = ctx.Web.GetFolderByServerRelativeUrl("/sites/dev/mydoc1/subfolder");
            ctx.Load(Parentfolder);
            ctx.Load(Parentfolder.Files);
            ctx.ExecuteQuery();
            if (Parentfolder.Files.Count>1)
            {
                foreach (File file in Parentfolder.Files)
                {
                    if (file.Name == "Test.jpg")
                    {
                        file.DeleteObject();
                        ctx.ExecuteQuery();
                    }
                }
            }

谢谢

最好的问候