Programming, VB.NET, Software, Visual Studio 2010, .Net, Visual Studio 2013, SharePoint, Tool

Get the Total Number of Documents in a SharePoint Site Collection Library and its Sub Folders using .NET

List the Total Number of Documents in a Library and its Sub Folders
List the Total Number of Documents in a Library and its Sub Folders

One thing that you often need and that is quite annoying to get in a default SharePoint installation is the total number of files in a document library and all of its sub-folders. The most common advice is to open the explorer view and then navigate through the folders and add up the file totals, but this becomes tedious really quickly if you have a library with numerous files and folders. Another option is to write a PowerShell script, but then you need to be on the SharePoint server itself and with an account that doesn’t have access to the document libraries. Also, in the case of PowerShell, you’ll quickly become annoyed with the many bad script examples out there that don’t work and end up taking your time.

So what to do? Well, the simplest way is just to start up Visual Studio on your PC and write a basic Windows forms application. See the image at the start of this article for the user interface (UI) I put together. It should work well with the code example below.

In the UI, there is a button to run the search and text boxes to enter the URL of your SharePoint Site Collection, and another text box where you enter the name of your document library. There are also two text boxes to show the results: the first shows the grand total of documents in the library, and the second lists them all in one big list.

Summing up the Important Parts of the Code

To briefly skim some of the code highlights, you need to:

  1. Create a new ClientContext instance where we pass in the URL of the SharePoint site collection.
  2. Then get the document library that you are interested in using: clientContext.Web.Lists.GetByTitle(tbLibraryName.Text)
  3. You can then get the full list of files in the document library and its sub-folders using a CamlQuery where you set the scope to be Recursive
  4. After you execute your CamlQuery, you can get the total number of files using .Count on the ListItemCollection you are using to store the file information
  5. Also, you can loop through each item in the ListItemCollection and derive information about each file (like getting the file name using FieldValues(“FileLeafRef”) )

The Code in VB.NET

Here is the VB.NET code for a classic Windows form application. Naturally you need to first add in references to SharePoint libraries in your project before you can begin to code. Here is an article where I explain how to add these references.

Imports Microsoft.SharePoint.Client
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        tbTotalDocs.Text = ""
        tbDocumentList.Text = ""
        Using clientContext As New ClientContext(tbURL.Text)
            Dim docList As List = clientContext.Web.Lists.GetByTitle(tbLibraryName.Text)
            Dim camlQuery As New CamlQuery()
            camlQuery.ViewXml =  "<View Scope='Recursive'><Query><Where><Eq><FieldRef Name='FSObjType' /><Value Type='Lookup'>0</Value></Eq></Where></Query></View>"
            Dim listItems As ListItemCollection = docList.GetItems(camlQuery)
            tbTotalDocs.Text = listItems.Count
            For Each listItem As ListItem In listItems
                Dim strName As String = listItem.FieldValues("FileLeafRef").ToString()
                tbDocumentList.Text &= strName & ControlChars.CrLf
        End Using
    End Sub
End Class

The Code in C#

Here’s the same code, but auto code-converted into C#. If there is a problem, just use the VB code above, because I wrote and tested the app only in VB.NET:

private void Button1_Click(object sender, EventArgs e) {
        tbTotalDocs.Text = "";
        tbDocumentList.Text = "";
        Using ClientContext clientContext = new ClientContext(tbURL.Text){
        List docList = clientContext.Web.Lists.GetByTitle(tbLibraryName.Text);
        CamlQuery camlQuery = new CamlQuery();
        camlQuery.ViewXml = "<View Scope=\'Recursive\'><Query><Where><Eq><FieldRef Name=\'FSObjType\' /><Value Type=\'Lookup\'>0</Value></Eq></Where></Query></View>";
        ListItemCollection listItems = docList.GetItems(camlQuery);
        tbTotalDocs.Text = listItems.Count;
        foreach (ListItem listItem in listItems) {
            string strName = listItem.FieldValues("FileLeafRef").ToString();
            (strName + ControlChars.CrLf);

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s