Get all document from all folder and subfolder from document library SharePoint

Get all document from all folder and subfolder from document library SharePoint

Get all document from all folder and subfolder from document library SharePoint. In this post, I describe you to Programmatically Get all document from all folder and subfolder from document library SharePoint. If you need to create or delete document library or list, follow my post Create and Delete SharePoint List, Document Library Programmatically using CSOM.

[AdSense-A]

public void GetDocument(string documentName)
        {
            try
            {

                 List<DocumentStorageData> DocumentStorageData_List = new List<DocumentStorageData>();
                    string siteUrl = "Your Site Collections URL";
                   
                    using (SPSite spsite = new SPSite(siteUrl))
                    {
                        using (SPWeb spweb = spsite.OpenWeb())
                        {
                            SPList sharedDocumentsList = spweb.Lists[documentLibraryName];
                            SPQuery camlQuery = new SPQuery();
                            camlQuery.ViewXml =
                                @"<View Scope='Recursive' />";
                            SPListItemCollection listItems =
                                sharedDocumentsList.GetItems(camlQuery);
                            DataTable dt = new DataTable();
                            dt = listItems.GetDataTable();
                            foreach (SPListItem item in listItems)
                            {
                                string FileName = item["Name"].ToString();
                                string FileURL = siteUrl + "/" + item.File.Url;
                                string Modified = item["Modified"].ToString();
                                string ModifiedBy = item["Modified By"].ToString();
                                string Created = item["Created"].ToString();
                                string CreatedBy = item["Created By"].ToString();
                                DocumentStorageData_List.Add(new DocumentStorageData
                                {
                                    FileName = FileName,
                                    FileURL = FileURL,
                                    Modified = Modified,
                                    ModifiedBy = ModifiedBy.Split('#').Count() > 0 ? ModifiedBy.Split('#')[1].ToString() : ModifiedBy,
                                    Created = Created,
                                    CreatedBy = CreatedBy.Split('#').Count() > 0 ? CreatedBy.Split('#')[1].ToString() : CreatedBy,
                                    FileName1 = FileName.Trim().ToUpper()
                                });
                            }
                            List<DocumentStorageData> DocumentStorageData_FinalList = new List<DocumentStorageData>();
                            DocumentStorageData_FinalList = (from DocumentStorageData_ListItem in DocumentStorageData_List where DocumentStorageData_ListItem.FileName1.Contains(documentName.Trim().ToUpper()) select DocumentStorageData_ListItem).ToList();
                            

                        }
                    }
                
            }
            catch (Exception ex)
            { }
        }

 public class DocumentStorageData
        {
            public DocumentStorageData()
            {
                FileName = string.Empty;
                FileURL = string.Empty;
                Modified = string.Empty;
                ModifiedBy = string.Empty;
                Created = string.Empty;
                CreatedBy = string.Empty;
                FileName1 = string.Empty;
            }
            public string FileName { get; set; }
            public string FileURL { get; set; }
            public string Modified { get; set; }
            public string ModifiedBy { get; set; }
            public string Created { get; set; }
            public string CreatedBy { get; set; }
            public string FileName1 { get; set; }


        }

 


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error: Content is protected !!