Tuesday, July 21, 2015

Get all folders(subfolder) files in a list/Library-Report- C#

Get all folders(sub folders) files/itmes in a list/Library Report


        static void getAllWebs(String siteURL)
        {
            StreamWriter SW;
            SW = File.AppendText("C:\\POS_FilesIDandURL.csv");
            SW.WriteLine("FileURL,FileID");
            using (SPSite site = new SPSite(siteURL))
            {
                foreach (SPWeb web in site.AllWebs)
                {
                    Console.WriteLine(web.Url);
                    AllItemsInADocLibrary(web.Url, SW);
                }
            }
            SW.Close();
        }

        static void AllItemsInADocLibrary(string webURL, StreamWriter SW)
        {
            string listName = string.Empty;


            using (SPSite site = new SPSite(webURL))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    if (web.Title.ToString().Contains('_'))
                    {
                        listName = "DeptDocLibrary_" + web.Title.ToString().Split('_')[1];
                        SPList list = web.Lists.TryGetList(listName);
                        if (list != null)
                        {
                            SPView Myview = list.Views["All Documents"];

                            SPQuery oQuery = new SPQuery(Myview);
                            oQuery.ViewAttributes = "Scope=\"Recursive\"";
                            SPListItemCollection itemColl = list.GetItems(oQuery);

                            foreach (SPListItem item in itemColl)
                            {
                                SW.WriteLine(item.Url.Replace(",", " ") + "," + item.ID);
                            }
                        }
                        else
                        {
                            Console.WriteLine("List :{0} - Not found in the web : {1} ", listName, web.Title);
                        }

                    }
                }
            }

        }

No comments:

Post a Comment