SharePoint Microsoft.SharePoint.Client.CamlQuery递归返回文件夹(包括子文件夹)

时间:2018-02-06 00:49:52

标签: sharepoint directory

我试图从SharePoint网站撤回所有文件夹和子文件夹(可以有任何数字)。我不想要文件(可能有数千个),所以我基本上只想构建一个文件夹层次结构。另外,我只想要用户创建的文件夹和主要文件"文件夹,而不是所有系统文件夹。

那就是说,我发现下面的例子我觉得应该有用,但是当我把它减少到文件夹时我只得到顶级文件夹:

https://stackoverflow.com/questions/16652288/sharepoint-client-get-all-folders-recursively

这是当前代码的状态。我可能只是在负载上缺少一些东西(如表达式?):

    public static void LoadContent(Microsoft.SharePoint.Client.Web web, out Dictionary<string, IEnumerable<Microsoft.SharePoint.Client.Folder>> listsFolders)
    {
        listsFolders = new Dictionary<string, IEnumerable<Microsoft.SharePoint.Client.Folder>>();
        var listsItems = new Dictionary<string, IEnumerable<Microsoft.SharePoint.Client.ListItem>>();

        var ctx = web.Context;
        var lists = ctx.LoadQuery(web.Lists.Where(l => l.BaseType == Microsoft.SharePoint.Client.BaseType.DocumentLibrary));
        ctx.ExecuteQuery();

        foreach (var list in lists)
        {
            var items = list.GetItems(Microsoft.SharePoint.Client.CamlQuery.CreateAllFoldersQuery());
            ctx.Load(items);
            listsItems[list.Title] = items;
        }
        ctx.ExecuteQuery();

        foreach (var listItems in listsItems)
        {
             listsFolders[listItems.Key] = listItems.Value.Where(i => i.FileSystemObjectType == Microsoft.SharePoint.Client.FileSystemObjectType.Folder).Select(i => i.Folder);
        }
    }

更新

只是为了帮助那些可能只想将主文件夹和子文件夹作为网址列表的其他人,这里是最终的代码。我怀疑它可以简化,但它正在工作。在下面的帮助之后的诀窍是获得&#34; root&#34;文件夹路径,需要单独的查询。我认为这可以证明更容易获得文件夹 - &gt;子文件夹,但我有文件夹 - &gt;子文件夹 - &gt;子文件夹和此解决方案获取最后一个子文件夹以及根文件夹。

    public static void LoadContent(Microsoft.SharePoint.Client.Web web, List<String> foldersList)
    {
        Dictionary<string, IEnumerable<Folder>>  listsFolders = new Dictionary<string, IEnumerable<Folder>>();
        var listsItems = new Dictionary<string, IEnumerable<ListItem>>();

        var ctx = web.Context;
        var lists = ctx.LoadQuery(web.Lists.Include(l => l.Title).Where(l => l.BaseType == BaseType.DocumentLibrary && !l.Hidden && !l.IsCatalog && !l.IsSiteAssetsLibrary));
        ctx.ExecuteQuery();

        foreach (var list in lists)
        {
            ctx.Load(list.RootFolder);
            ctx.ExecuteQuery();
        }

        foreach (var list in lists)
        {
            if (list.Title != "Form Templates" && list.Title != "MicroFeed" && list.Title != "Site Assets" && list.Title != "Site Pages")
            {
                foldersList.Add(list.RootFolder.ServerRelativeUrl);

                var items = list.GetItems(CamlQuery.CreateAllFoldersQuery());
                ctx.Load(items, icol => icol.Include(i => i.FileSystemObjectType, i => i.Folder));
                listsItems[list.Title] = items;
            }
        }
        ctx.ExecuteQuery();

        foreach (var listItems in listsItems)
        {
            listsFolders[listItems.Key] = listItems.Value.Where(i => i.FileSystemObjectType == FileSystemObjectType.Folder).Select(i => i.Folder);
        }

        foreach (var item in listsFolders)
        {
            IEnumerable<Folder> folders = item.Value;
            foreach (Folder folder in folders)
            {
                foldersList.Add(folder.ServerRelativeUrl);
            }
        }
    }

返回的示例:

enter image description here

2 个答案:

答案 0 :(得分:1)

1)在提供的示例中,要返回Folder对象,需要明确包含它,否则会出现exception,所以请替换该行:

ctx.Load(items);

使用:

ctx.Load(items, icol => icol.Include(i => i.FileSystemObjectType, i => i.Folder));

2)&#34; system&#34;可以像这样排除图书馆:

var lists = ctx.LoadQuery(web.Lists.Where(l => !l.Hidden && !l.IsCatalog && !l.IsSiteAssetsLibrary));

已修改示例

public static void LoadContent(Web web, out Dictionary<string, IEnumerable<Folder>> listsFolders)
{
        listsFolders = new Dictionary<string, IEnumerable<Folder>>();
        var listsItems = new Dictionary<string, IEnumerable<ListItem>>();

        var ctx = web.Context;
        var lists = ctx.LoadQuery(web.Lists.Include(l =>l.Title).Where(l => l.BaseType == BaseType.DocumentLibrary && !l.Hidden && !l.IsCatalog && !l.IsSiteAssetsLibrary));
        ctx.ExecuteQuery();

        foreach (var list in lists)
        {
            var items = list.GetItems(CamlQuery.CreateAllFoldersQuery());
            ctx.Load(items, icol => icol.Include(i => i.FileSystemObjectType, i => i.Folder));
            listsItems[list.Title] = items;
        }
        ctx.ExecuteQuery();

        foreach (var listItems in listsItems)
        {
            listsFolders[listItems.Key] = listItems.Value.Where(i => i.FileSystemObjectType == FileSystemObjectType.Folder).Select(i => i.Folder);
        }
}

答案 1 :(得分:0)

试试这个。

var lists = ctx.LoadQuery(ctx.Web.Lists.Where(l => l.BaseType == BaseType.DocumentLibrary));                
                ctx.ExecuteQuery();
                foreach (var list in lists)
                {
                    Console.WriteLine(list.Title);
                    ListItemCollection listitems = list.GetItems(CamlQuery.CreateAllFoldersQuery());
                    ctx.Load(listitems, items => items.Include(item => item.Id,item=>item.Folder));
                    ctx.ExecuteQuery();
                    foreach (var item in listitems)
                    {
                        Console.WriteLine(item.Folder.ServerRelativeUrl);
                    }                                        
                }