I had some requirements from client, for fulfilling that I had to write a code for getting list of all documents from folders/sub-folders from a SharePoint library.
Below is the code which I used:
===========================MAIN METHOD================================
===========================ITERATE FOLDER METHOD====================
Below is the code which I used:
===========================MAIN METHOD================================
private void OCR_Document() { try { SPWebApplication webApp = SPWebApplication.Lookup(new Uri("webapplication URL")); //iterates in each site and subsites foreach (SPSite site in webApp.Sites) { foreach (SPWeb web in site.AllWebs) { //iterates in each lists/libraries foreach (SPList list in web.Lists) { //selects only document libraries if (Convert.ToString(list.BaseType) == "DocumentLibrary") { //check if document library contains folder if (list.Folders.Count > 0) { SPQuery query = new SPQuery(); query.Folder = list.RootFolder; SPListItemCollection itemCollection = web.Lists[list.RootFolder.ParentListId].GetItems(query); foreach (SPListItem subitem in itemCollection) { //iterate in each folder-subfolder, all folders will come into this loop if (subitem.ContentType.Name == "Folder") { iterateFolder(subitem.Folder); } else { //here you will get only files in subitem varialbe //write your code here for files } } } else { foreach (SPFile file in list.RootFolder.Files) { //here you will get only files //write your code here for files } } } } } } } catch { throw; } }
private void iterateFolder(SPFolder folder) { if (folder.SubFolders != null) { //check for subfolders foreach (SPFolder item in folder.SubFolders) { //scroll through each subfolder and each child folder iterateFolder(item.SubFolders.Folder); } } foreach (SPFile file in folder.Files) { //here you will get only files //write your code here for files } }
No comments:
Post a Comment