I want to retrieve all the files which are last modified in a particular document library. I will not be able to find out that how many folders are there in the document library. User can select any of its library.
I want to show only documents in descending order of modified date. Currently I am checking while retrieving items if the current item is folder, if it is folder I loop through that folder and retrieve all files from that. If that folder has also subfolder I will have to again loop through that. I don't know till how many level should I go.
Below is the code which I am using right now:
int temp = 0;
SPList cList = spWeb.Lists[listName];
SPQuery spQuery = new SPQuery();
spQuery.Query = "<OrderBy><FieldRef Name='Modified' Ascending='False' /></OrderBy>";
spQuery.RowLimit = 4;
SPListItemCollection collection = cList.GetItems(spQuery);
if (collection.Count > 0)
{
foreach (SPListItem cItem in collection)
{
if (cItem.Folder == null)
{
do something...!
temp++;
}
else
{
//SPList cList_temp = spWeb.Lists[listName];
SPQuery spQuery_temp = new SPQuery();
spQuery_temp.Query = "<OrderBy><FieldRef Name='Modified' Ascending='False' /></OrderBy>";
spQuery_temp.Folder = cItem.Folder;
spQuery_temp.RowLimit = 4;
SPListItemCollection collection_temp = cList.GetItems(spQuery_temp);
if (collection_temp.Count > 0)
{
foreach (SPListItem item in collection_temp)
{
if (temp < 4)
{
if (item.Folder != null)
{
continue;
}
do something...!
temp++;
}
}
}
}
if (temp == 4)
break;
}
}
Using this code I am able to get only first level folder's files. I want to retrieve latest 4 documents from the library. That's why I have used temp
variable.
Is there any batter option than this?
Thank you in advance...