メインコンテンツへスキップ
以下の例は、フォルダツリーのJSONレプリゼンテーションを作成する方法を示しています。フォルダツリーは、フォルダの名前とそのフォルダ内にあるすべてのサブフォルダで構成されます。 以下のサンプルでは、先頭のルートフォルダと、コードでトラバースする最大深度を指定できます。また、初期化されたSDKクライアントを渡すことができるため、どのユーザーが認証されるかを構成することもできます。
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Box.V2;
using Box.V2.Auth;
using Box.V2.Config;
using Box.V2.Converter;
using Box.V2.Exceptions;
using Box.V2.JWTAuth;
using Box.V2.Models;
using Newtonsoft.Json;

namespace BoxPlayground
{
    public class Program4
    {
        static void Main(string[] args)
        {
            ExecuteMainAsync().Wait();
        }

        public class BoxFolderTreeBuilder
        {
            public BoxClient BoxClient { get; set; }
            public string RootFolderId { get; set; }
            public int MaxDepth { get; set; }

            public BoxFolderTreeBuilder(BoxClient boxClient, string rootFolderId = "0", int maxDepth = -1)
            {
                this.BoxClient = boxClient;
                this.RootFolderId = rootFolderId;
                this.MaxDepth = maxDepth;
            }

            public async Task<BoxFolderTree> BuildFolderTreeWithFlatLists()
            {
                var tree = new BoxFolderTree
                {
                    RootId = this.RootFolderId,
                    Files = new List<BoxFolderTreeItem>(),
                    Folders = new List<BoxFolderTreeFolder>()
                };
                var rootFolderItems = await this.BoxClient.FoldersManager.GetFolderItemsAsync(this.RootFolderId, limit: 1000, autoPaginate: true);
                var rootFolderChildren = new List<BoxFolderTreeFolder>();
                foreach (var item in rootFolderItems.Entries)
                {
                    var folderTreeItem = new BoxFolderTreeItem(item);
                    folderTreeItem.Path = $"{this.RootFolderId}";
                    if (item.Type == "file")
                    {
                        tree.Files.Add(folderTreeItem);
                    }
                    else if (item.Type == "folder")
                    {
                        var childFolder = new BoxFolderTreeFolder(folderTreeItem);
                        tree.Folders.Add(new BoxFolderTreeFolder(folderTreeItem));
                        rootFolderChildren.Add(childFolder);
                    }
                }
                tree = await Dive(tree, rootFolderChildren, 1);
                return tree;
            }

            private async Task<BoxFolderTree> Dive(BoxFolderTree tree,
                List<BoxFolderTreeFolder> children, int currentDepth)
            {
                if (InTooDeep(currentDepth))
                {
                    return tree;
                }
                else
                {
                    currentDepth++;
                    var additionalChildren = new List<BoxFolderTreeFolder>();
                    foreach (var child in children)
                    {
                        var folderItems = await this.BoxClient.FoldersManager.GetFolderItemsAsync(child.Item.Id, limit: 1000, autoPaginate: true);
                        var foundFolder = tree.Folders.FindIndex((f) =>
                        {
                            return f.Item.Id == child.Item.Id;
                        });
                        foreach (var item in folderItems.Entries)
                        {
                            if (foundFolder >= 0)
                            {
                                tree.Folders[foundFolder].Children.Add(item);
                            }
                            var folderTreeItem = new BoxFolderTreeItem(item);
                            folderTreeItem.Path = $"{child.Path}/{child.Item.Id}";
                            if (item.Type == "file")
                            {
                                tree.Files.Add(folderTreeItem);
                            }
                            else if (item.Type == "folder")
                            {
                                var childFolder = new BoxFolderTreeFolder(folderTreeItem);
                                tree.Folders.Add(new BoxFolderTreeFolder(folderTreeItem));
                                additionalChildren.Add(childFolder);
                            }
                        }
                    }
                    if (additionalChildren.Count == 0)
                    {
                        return tree;
                    }
                    else
                    {
                        return await Dive(tree, additionalChildren, currentDepth);
                    }
                }
            }

            private bool InTooDeep(int depthCount)
            {
                if (this.MaxDepth < 0)
                {
                    return false;
                }
                else
                {
                    return (depthCount >= this.MaxDepth);
                }
            }
            public class BoxFolderTreeItem
            {
                [JsonProperty(PropertyName = "item")]
                public BoxItem Item { get; set; }

                [JsonProperty(PropertyName = "path")]
                public string Path { get; set; }
                public BoxFolderTreeItem(BoxItem item)
                {
                    Item = item;
                }
            }
            public class BoxFolderTreeFolder : BoxFolderTreeItem
            {
                public BoxFolderTreeFolder(BoxFolderTreeItem item) : base(item.Item)
                {
                    this.Path = item.Path;
                }

                [JsonProperty(PropertyName = "children")]
                public List<BoxItem> Children { get; set; } = new List<BoxItem>();
            }
            public class BoxFolderTree
            {
                [JsonProperty(PropertyName = "rootId")]
                public string RootId { get; set; }
                [JsonProperty(PropertyName = "files")]
                public List<BoxFolderTreeItem> Files { get; set; }

                [JsonProperty(PropertyName = "folders")]
                public List<BoxFolderTreeFolder> Folders { get; set; }

                public string writeJSON()
                {
                    var converter = new Box.V2.Converter.BoxJsonConverter();
                    return converter.Serialize<BoxFolderTreeBuilder.BoxFolderTree>(this);
                }
            }
        }
        private static async Task ExecuteMainAsync()
        {
            using (FileStream fs = new FileStream($"./config.json", FileMode.Open))
            {
                var session = new BoxJWTAuth(BoxConfig.CreateFromJsonFile(fs));
                var serviceAccountClient = session.AdminClient(session.AdminToken());
                var folderTreeBuilder = new BoxFolderTreeBuilder(serviceAccountClient, rootFolderId: "37477903736");
                var tree = await folderTreeBuilder.BuildFolderTreeWithFlatLists();
                System.Console.WriteLine(tree.writeJSON());
            }
        }
    }
}