欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Sharepoint回收站文件还原,还原其他用户删除的文件

程序员文章站 2022-07-03 12:29:30
...

需求:有两个site collection admin权限的用户user1和user2,user2删除OtherUserDelete.txt文件,user1进行还原,其中user1的回收站里面有两个文件CurrentUserDelete.txt(user1删除)和OtherUserDelete.txt(user2删除),这里只考虑first stage一级回收站的情况

Sharepoint回收站文件还原,还原其他用户删除的文件

dll引用:主要是调用Microsoft.SharepointOnline.CSOM中的Client api

问题:回收站分为两个,一个是End User的只有自己删除的文件,另外一个是site collection级别的回收站,但是Web.Recyclebin里面只能得到当前用户删除的文件,如下面代码

using (ClientContext clientContext = new ClientContext("https://youserver"))
                {
                    string pass = "yourpassword";
                    var securepassword = new SecureString();
                    foreach (char c in pass.ToCharArray())
                    {
                        securepassword.AppendChar(c);
                    }
                    clientContext.Credentials = new SharePointOnlineCredentials("aaa@qq.com", securepassword);
                    var deletedItems = new List<RecycleBinItem>();
                    try
                    {
                        RecycleBinItem deletedItem = null;
                        Web web = clientContext.Site.RootWeb;
                        clientContext.Load(web);
                        clientContext.Load(web.RecycleBin, items => items.Include(
                            oItem => oItem.Title,
                            oItem => oItem.Id,
                            oItem => oItem.Author,
                            oItem => oItem.DeletedBy,
                            oItem => oItem.DirName,
                            oItem => oItem.DeletedDate
                            ));
                        clientContext.ExecuteQuery();                      
                        var recycleBinItems = web.RecycleBin;
                        for (int i = recycleBinItems.Count; i > 0; i--)
                        {
                            RecycleBinItem item = recycleBinItems[i - 1];
                            deletedItems.Add(item);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }

                }

1.通过Web.GetRecycleBinItemsByQueryInfo(RecycleBinQueryInformation query)拿到回收站所有的文件,其中属性 ShowOnlyMyItems要设置为false才能拿到其他用户删除的文件

RecycleBinQueryInformation queryInformation = new RecycleBinQueryInformation
                        {
                            ShowOnlyMyItems = false
                        };
                        var recycleBinItems = web.GetRecycleBinItemsByQueryInfo(queryInformation);
                        clientContext.Load(recycleBinItems, items => items.Include(
                            oItem => oItem.Title,
                            oItem => oItem.Id,
                            oItem => oItem.Author,
                            oItem => oItem.DeletedBy,
                            oItem => oItem.DirName,
                            oItem => oItem.DeletedDate,
                            oItem => oItem.DirNamePath
                            ));
                        clientContext.ExecuteQuery();
foreach (RecycleBinItem item in recycleBinItems)
                        {
                            string guid = item.Id.ToString();
                            //你想要还原的文件guid
                            if (item.Id.ToString().Equals("424d868f-f08a-4f56-809b-37466d8c2f79"))
                            {
                                deletedItem = item;
                                item.Restore();
                                clientContext.ExecuteQuery();
                                break;
                            }
                        }

2.上面方法成功拿到user2删除的文件,但是还原的时候出问题了,报错:Value does not fall within the expected range.

3.解决办法是通过RecycleBinItemCollection.GetById(Guid id)方法拿到想要还原的user2删除的文件RecycleBinItem,然后进行还原就成功还原了

RecycleBinItem deletedItem = null;
                        Web web = clientContext.Web;                       
                        RecycleBinQueryInformation queryInformation = new RecycleBinQueryInformation
                        {
                            ShowOnlyMyItems = false
                        };
                        var recycleBinItems = web.GetRecycleBinItemsByQueryInfo(queryInformation);
                        clientContext.Load(recycleBinItems, items => items.Include(
                            oItem => oItem.Title,
                            oItem => oItem.Id,
                            oItem => oItem.Author,
                            oItem => oItem.DeletedBy,
                            oItem => oItem.DirName,
                            oItem => oItem.DeletedDate,
                            oItem => oItem.DirNamePath
                            ));
                        clientContext.ExecuteQuery();                        
                        deletedItem =recycleBinItems.GetById(new Guid("yourfileid"));
                        clientContext.Load(deletedItem, w => w.DirName);                        
                        deletedItem.Restore();
                        clientContext.ExecuteQuery();