I need all the documents from the Document library.

2 Answers 2


You would loop through the library like a normal SPList and then cast the file to a SPFile type

foreach (SPListItem i in currentList.Items)
    SPFile fi = currentList.GetItemById(i.ID).File;

If there are folders present in the library containing the files and the requirement is to get those files as well, then recursive scope can be defined in SPQuery. Example:

SPList oList = oWebsite.Lists["Shared Documents"];
SPQuery oQuery = new SPQuery();
oQuery.Query = strQuery;
oQuery.ViewAttributes = "Scope=\"Recursive\""; 
SPListItemCollection collItemsRoot = oList.GetItems(oQuery);

foreach (SPListItem oItem in collItemsRoot)
    if (oItem.FileSystemObjectType == SPFileSystemObjectType.File)
        string fileName = oItem.File.Name;
  • what is strQuery in 2nd answer
    – user55908
    Commented Jun 7, 2016 at 9:04
  • strQuery will be your Caml query...
    – P S
    Commented Jun 7, 2016 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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