How to read items from folder in SharePoint List/Library Programmatically C#
-
08-10-2020 - |
Question
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?
Solution
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.
OTHER TIPS
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"];
}
}
}
}
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);
}
}
}
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
}
}
}
}
}
}
}
}
}
}
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/>";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange