2

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...

2

On your SPQuery object, you can set the scope in the ViewAttributes property, as documented here: https://msdn.microsoft.com/en-us/library/office/microsoft.sharepoint.spquery.viewattributes.aspx

...
SPQuery spQuery_temp = new SPQuery();
spQuery_temp.Query = "<OrderBy><FieldRef Name='Modified' Ascending='False' /></OrderBy>";
spQuery_temp.ViewAttributes = "Scope=\"Recursive\"";
...

With this recursive scope setting, the query will retrieve all items from the target location regardless of which subfolder they are in.

2

try this one:

 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/>”; 
    }

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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