How To Programmatically Enumerate SharePoint Document Library Folder
I am working on project where I needed to programmatically enumerate the list of documents stored in the SharePoint Document Library. As it quite often turns out this task is fairly trivial once you figure out the right approach. Let me provide a quick walk-through on how I accomplished this task.
Background
SharePoint provides various Web Services that allow a developer to extract all sorts of information regarding SharePoint. On of such Web Services is Site Data Service, and one of the methods of this Web Service is EnumerateFolder. Once you know this the reset is easy.
Walkthrough
Let's build a sample console application that will enumerate documents that are stored in a document library
In this example the library is stored under site archives, and the library name is testarchive.
1. Add a Web Reference to your console application
Each SharePoint site gets its own SiteData Web Service, so make sure to specify the correct path of your site in which the document library resides
2. Paste this code into the Main function
Dim srvSiteData As New localhost.SiteData
srvSiteData.Credentials = System.Net.CredentialCache.DefaultCredentials
Dim enArray() As localhost._sFPUrl
srvSiteData.EnumerateFolder("testarchive", enArray)
Dim en As localhost._sFPUrl
Dim out As String
For Each en In enArray
If Not en.IsFolder Then
out += en.Url.ToString() + " :: " + en.LastModified.ToString() + ControlChars.Lf
End If
Next en
Console.WriteLine(out)
Console.ReadLine()
Make sure to substitute "testarchive" with the name of your document library.
Also because I am running SharePoint on my local machine the reference to the Web Service is done through localhost, this will change if SharePoint is running remotely.
3. Test your solution. Your result should look something like this:
References:
EnumerateFolder Method on MSDN