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
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It only takes a minute to sign up.
Sign up to join this communityHi 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
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();
}
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();
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);
}
}