1

There is a image library that contains different folders. I am trying to fetch all the items with items count from inside the folders using CAML query but its fetching all items from root folder and items inside the folders without the folder names.

Any thoughts?

1
  • What is your exact goal? There are OOB webparts able to display images. Why do you want to retrieve them ?
    – Steve B
    Sep 23, 2016 at 15:33

5 Answers 5

4

Try below code.

using (SPSite site = new SPSite("Your Site URL"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {

                        if (web != null)
                        {
                            SPList list = web.Lists["Your List Name"];
                            SPListItemCollection items = GetListItems(list);
                            if (items.Count > 0)
                            {
                                //This will give your all the folders in root folder
                                foreach (SPFolder folder in list.RootFolder.SubFolders)
                                {
                                    //This is the default folder which is not visible so skip this
                                    if (folder.Name.ToLower() != "_w")
                                    {
                                        SPQuery query = new SPQuery();
                                        query.Folder = folder;
                                        SPListItemCollection listitem = list.GetItems(query);
                                        if (listitem.Count > 0)
                                        {
                                            //This will return you the current folder name
                                            string Name = folder.Name;
                                            //This will return you the total number of items in that folder
                                            int count = folder.ItemCount;
                                            //Thi will return you the Title of the first Item. You Can also use loop here to iterate through all items
                                            string Title = listitem[0]["Title"];
                                        }
                                    }
                                }
                            }

                        }

                    }

                }

Hope it helps and fill up your needs.

4
  • Well explained. Let me try this. Apr 21, 2016 at 10:17
  • Thank you for this. It worked fine and exactly what i am looking for. How i can get other fields of the items inside the folder? I want to read all items. Apr 21, 2016 at 10:22
  • Simply use listitem[0]["Inner Name of Your Custom Property"]; to retrieve the custom field value. To read all items inside the folder, you need to put a loop that should iterate through SPListItemCollection i.e (listitem) object and the use the same listitem["Index No"]["Field Name"] way to retrieve the properties. Hope it works for you. Apr 21, 2016 at 10:26
  • Syntax error on GetListItems
    – user75190
    Apr 12, 2018 at 21:12
3
using (SPSite site = new SPSite("SiteURL"))
  {
    using (SPWeb web = site.OpenWeb())
     {
       if (web != null)
         {
           SPQuery query = new SPQuery();
           query.ViewAttributes = "Scope='Recursive'";
           query.RowLimit = 2000;
           CamlQuery caml = new CamlQuery();
       caml = "<OrderBy Override='TRUE'><FieldRefName='ID'/></OrderBy>"
           query.Query = caml ;
           SPList list = web.Lists["MyListName"];
               SPListItemCollection listitem = list.GetItems(query);

                 foreach(SPListItem item in listitem)
                     {
                        //Access your column data

            int ID = item["ID"];
            string MyColumn1 = item["ColumnName1"];
            //For lookup columns
    string LookupColumnData = item.Properties["ColumnName2"];
                     }

                    }

                }

            }
2

Try the following code to get the items based on the folder,

using(SPSite site = new SPSite("site url"))
{
  using(SPWeb web = site.OpenWeb())
  {
    SPFolder folder = web.GetFolder("/Docs/folder1");
    if(folder.ItemCount > 0)
    {
      SPList list = web.Lists.TryGetList("ListName");
      SPQuery query = new SPQuery();
      query.Folder = folder;
      SPListItemCollection listitem = list.GetItems(query);
    }
  }
}
1
  • This one is good but it do not return me all folder names and number of items in that folder. Thanks for your thoughts. Apr 21, 2016 at 10:13
0
using (SPSite site = new SPSite("Your Site URL"))
            {
                using (SPWeb web = site.OpenWeb())
                {

                    if (web != null)
                    {
                        SPList list = web.Lists["CPR"];
                        SPQuery spQuery = new SPQuery();
                        SPListItemCollection items = web.Lists["CPR"].GetItems(spQuery);
                        //SPListItemCollection spListItemCollection = _objweb.Lists["CPR"].GetItems(spQuery);
                        if (items.Count > 0)
                        {
                            //This will give your all the folders in root folder
                            foreach (SPFolder folder in list.RootFolder.SubFolders)
                            {
                                //This is the default folder which is not visible so skip this
                                if (folder.Name.ToLower() != "_w")
                                {
                                    SPQuery query = new SPQuery();
                                    query.Folder = folder;
                                    SPListItemCollection listitem = list.GetItems(query);
                                    if (listitem.Count > 0)
                                    {
                                        //This will return you the current folder name
                                        string Name = folder.Name;
                                        //This will return you the total number of items in that folder
                                        int count = folder.ItemCount;

                                        for (int i = 0; i < count; i++)
                                        {
                                           // SPFolder subFolder = list.RootFolder.SubFolders[i];
                                            SPFolderCollection subFoldersOfSubFolder = folder.SubFolders;

                                            if (subFoldersOfSubFolder.Count > 0)
                                            {
                                                for (int j = 0; j < subFoldersOfSubFolder.Count; j++)
                                                {
                                                    SPFolder specificSubFolder = subFoldersOfSubFolder[j];
                                                    /*
                                                        At this point you could use properties like Name, 
                                                        ServerRelativeUrl or UniqueId of the SubFolder class to get 
                                                        the information you need.
                                                    */
                                                    if (specificSubFolder.Name == "Unfunded Report")
                                                    {

                                                        SPFolderCollection subFoldersUnfunded = specificSubFolder.SubFolders;
                                                        for (int k = 0; k < subFoldersUnfunded.Count; k++)
                                                        {
                                                            SPFolder specificProfitCenterfolders = subFoldersUnfunded[k];

                                                            if (specificProfitCenterfolders.Name == "USBL")
                                                            {
                                                                SPFolderCollection subFoldersfyear = specificProfitCenterfolders.SubFolders;

                                                                for (int l = 0; l < subFoldersfyear.Count; l++)
                                                                {
                                                                    SPFolder fyear = subFoldersfyear[l];

                                                                    if (fyear.Name == "FY 2017")
                                                                    {
                                                                        SPFileCollection fileitems = fyear.Files;

                                                                        foreach (SPFile file in fileitems)
                                                                        {
                                                                            string filename = file.Name;
                                                                            string URl = file.Url;



                                                                        }
                                                                    }




                                                                }
                                                            }
                                                        }


                                                    }


                                                }
                                            }
                                            else
                                            {
                                                //If you get to here, it means that the sub-folder had no sub-folders
                                            }


                                        }
                                        //Thi will return you the Title of the first Item. You Can also use loop here to iterate through all items
                                        string Title = listitem[0]["Title"].ToString();


                                        if (Title == "Unfunded Report")
                                        {
                                            //SPFolder subFolder = list.RootFolder.SubFolder[i];
                                            SPFolder subFolder = list.RootFolder.SubFolders[0];
                                            SPFolderCollection subFoldersOfSubFolder = subFolder.SubFolders;

                                            if (subFoldersOfSubFolder.Count > 0)
                                            {
                                                for (int j = 0; j < subFoldersOfSubFolder.Count; j++)
                                                {
                                                    SPFolder specificSubFolder = subFoldersOfSubFolder[j];
                                                    /*
                                                        At this point you could use properties like Name, 
                                                        ServerRelativeUrl or UniqueId of the SubFolder class to get 
                                                        the information you need.
                                                    */
                                                }
                                            }
                                            else
                                            {
                                                //If you get to here, it means that the sub-folder had no sub-folders
                                            }

                                        }

                                    }
                                }
                            }
                        }

                    }

                }

            }
        }
2
  • If that kind of code went to Production environment, I pity the support team :) Sep 23, 2016 at 15:33
  • Can you change the code so that we can move it prod enov??? Sep 23, 2016 at 20:50
0

http://www.ktskumar.com/2009/07/retrieve-all-folders-from-list/

SPSite site = SPContext.Current.Site;
SPWeb web = SPContext.Current.Web;
SPList list = web.Lists["Shared Documents"];

SPQuery query = new SPQuery();
//Condition to check the item type is folder or not
query.Query = "<Where><Eq><FieldRef Name='FSObjType'/><Value Type='Lookup'>1</Value></Eq></Where>";
//Get all the items including subfolders from the list query.ViewAttributes = "Scope='RecursiveAll'";
//Retrieve the items based on Query SPListItemCollection items = list.GetItems(query);
string folderDetails="";
//Get the name and Url for the folder 
foreach (SPListItem item in items)
{
    folderDetails += "Folder Name:" + item.Name + "<br/>Folder URL:" + web.Url + "/" + item.Url + "<br/>"; 
}
1
  • This worked well but didn't returned me the number of items. Thank you for thoughts. Apr 21, 2016 at 10:38

Your Answer

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

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