Search This Blog

Friday, October 23, 2020

Get all files and folder from SharePoint - CSOM

using Microsoft.SharePoint.Client;

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Security;

using System.Text;

using System.Threading.Tasks;

 

namespace MovingSharePoint

{

    class Program

    {

        /// <summary>

        /// Get all files and folder from SharePoint

        /// </summary>

        /// <param name="args"></param>

        static void Main(string[] args)

        {

 

            using (var ctx = new ClientContext("http://spserver:1234/sites/dev/"))

            {

                Dictionary<string, IEnumerable<Folder>> listsFolders;

                Dictionary<string, IEnumerable<Microsoft.SharePoint.Client.File>> listsFiles;

                LoadContent(ctx.Web, out listsFolders, out listsFiles);

                //Print File results

                foreach (var result in listsFiles)

                {

                  //  Console.WriteLine("List: {0}", result.Key);

                    foreach (var file in result.Value)

                    {

                        Console.WriteLine("File: {0}", file.Name);

                    }

                }

                //Print folder results

                foreach (var result in listsFolders)

                {

                   // Console.WriteLine("List: {0}", result.Key);

                    foreach (var folder in result.Value)

                    {

                        Console.WriteLine("Folder: {0}", folder.Name);

                    }

                }

            }

            Console.ReadLine();

        }

        public static void LoadContent(Web web, out Dictionary<string, IEnumerable<Folder>> listsFolders, out Dictionary<string, IEnumerable<Microsoft.SharePoint.Client.File>> listsFiles)

        {

            listsFolders = new Dictionary<string, IEnumerable<Folder>>();

            listsFiles = new Dictionary<string, IEnumerable<Microsoft.SharePoint.Client.File>>();

            var listsItems = new Dictionary<string, IEnumerable<ListItem>>();

 

            var ctx = web.Context;

            var lists = ctx.LoadQuery(web.Lists.Where(l => l.BaseType == BaseType.DocumentLibrary));

            ctx.ExecuteQuery();

 

            foreach (var list in lists)

            {

                var items = list.GetItems(CamlQuery.CreateAllItemsQuery());

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

                listsItems[list.Title] = items;

            }

            ctx.ExecuteQuery();

 

            foreach (var listItems in listsItems)

            {

                listsFiles[listItems.Key] = listItems.Value.Where(i => i.FileSystemObjectType == FileSystemObjectType.File).Select(i => i.File);

                listsFolders[listItems.Key] = listItems.Value.Where(i => i.FileSystemObjectType == FileSystemObjectType.Folder).Select(i => i.Folder);

                Console.WriteLine("List: {0}", listItems.Key);

            }

        }

    }


No comments:

Post a Comment