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

  • Do you want to display its items? – Rishi Jagati Dec 9 '16 at 8:50
  • Yes i want to display it's items – JishinK Dec 9 '16 at 9:06
4

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();
}
  • 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 '16 at 9:49
  • Please provide more information. – Akshay Belure Dec 9 '16 at 9:51
  • have you provided credential? – Akshay Belure Dec 9 '16 at 9:53
  • yes able to connect the folder and all – JishinK Dec 9 '16 at 10:36
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();
  • Here am getting only the the id just like this – JishinK Dec 9 '16 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 '16 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.