0

Hi Friends i have a SharePoint site having more Document Library's i just want to display this as a Console application using SharePoint Client Object Model.

thanks in advance

2
  • Do you want to display its items? Dec 9, 2016 at 8:50
  • Yes i want to display it's items
    – JishinK
    Dec 9, 2016 at 9:06

3 Answers 3

0

Don't fetch all the files from a folder and do a Count() as stated in the accepted solution above. This will load a lot of (overhead) information you don't need. And with large libraries or folders 'Threshold exceeded' exceptions will be thrown.

Use the ItemCount property of the document library or the folders.

 foreach (var list in docLibs)
   {             
        Console.WriteLine(list.Title);
        context.Load(list.ItemCount);
        context.Load(list.RootFolder.Folders);
        context.ExecuteQuery();
        
        Console.WriteLine("---- library item count " + list.ItemCount);
        
        // this loop below can be simplified with one linq query. 
        // Kept it for simplicity reasons.
        foreach (Folder folder in list.RootFolder.Folders)
        {
          context.Load(folder, p=> p.ItemCount);
          context.ExecuteQuery();
          Console.WriteLine(folder.Name + "---- File count " + folder.ItemCount);
        }
     }
3

Following is the code to retrieve all document libraries with folders and file count.Hope it helps.

Console.WriteLine("Enter your user name (format: username@tenant.onmicrosoft.com)");
string userName = Console.ReadLine();
Console.WriteLine("Enter your password.");
SecureString password = GetPasswordFromConsoleInput();
using (ClientContext context = new ClientContext("Site Url"))
{
   context.Credentials = new SharePointOnlineCredentials(userName,password);
   Web web = context.Web;       
   var docLibs = context.LoadQuery(web.Lists.Where(l => l.BaseTemplate == 101));
   context.ExecuteQuery();

   foreach (var list in docLibs)
   {             
        Console.WriteLine(list.Title);
        context.Load(list.RootFolder.Folders);
        context.ExecuteQuery();
        foreach (Folder folder in list.RootFolder.Folders)
        {
          context.Load(folder.Files);
          context.ExecuteQuery();
          Console.WriteLine(folder.Name + "---- File count " + folder.Files.Count);
        }

     }
           Console.ReadLine();
}
5
  • getting error at the place context.ExecuteQuery() it displays An unhandled exception of type 'System.Net.WebException' occurred in Microsoft.SharePoint.Client.dll
    – JishinK
    Dec 9, 2016 at 9:49
  • Please provide more information. Dec 9, 2016 at 9:51
  • have you provided credential? Dec 9, 2016 at 9:53
  • yes able to connect the folder and all
    – JishinK
    Dec 9, 2016 at 10:36
  • With this code you will run into 'threshold exceeded' errors when you have a library with a large amount of documents.
    – Wout
    Oct 7, 2020 at 7:19
1

It will return you all document library data in application.

 using (ClientContext clientContext = new ClientContext("Your Site URL "))
                {
                    List docList = clientContext.Web.Lists.GetByTitle("Document Library Name");
                    clientContext.Load(docList); CamlQuery camlQuery = new CamlQuery();
                    camlQuery.ViewXml = "<View Scope='RecursiveAll'></View>";
                    ListItemCollection listItems = docList.GetItems(camlQuery);
                    clientContext.Load(listItems);
                    clientContext.ExecuteQuery();
                    foreach (ListItem listItem in listItems)
                    Console.WriteLine("Id: {0} ", listItem.Id);
                }
                Console.ReadLine();
2
  • Here am getting only the the id just like this
    – JishinK
    Dec 9, 2016 at 8:59
  • Id 1 id2 ...... id 15 i want the folder name and it's file count can u help me
    – JishinK
    Dec 9, 2016 at 8:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.