1

I have a document library which has a two level hierarchy of folders.I have to get the latest week.pdf which is added in Folder2/latest month(every month they add new folder in Folder2 and they add new week.pdf every week).I have tried to get that using server object model,but couldn't.

enter image description here

I am able to get the latest document from all the folders with in the document library using the following code.But I have to get the latest doc from the nested folders.

    using (SPSite site = new SPSite(SPContext.Current.Web.Url))

    {
        using (SPWeb web = site.OpenWeb())

        {
            int id = 0;

            SPList items = web.Lists["Resources"];

            SPQuery query = new SPQuery();

            String camlquery = "<Where><Geq><FieldRef Name='Created' /><Value IncludeTimeValue='FALSE' Type='DateTime'>" + SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now) + "</Value></Geq></And></Where>";

            query.ViewAttributes =
            "Scope = 'Recursive'";

            query.ViewFields =
            "<FieldRef Name='Created' /><FieldRef Name='Title' /><FieldRef Name='Name' />";

            query.Query = camlquery;

            query.RowLimit = 1;

            SPListItemCollection ic = items.GetItems(query);

            //Some code here

        }
    }                                                          
5
  • Just so I understand the requirement correctly, in June you need to get the weekN.pdf (where N is last week in month...some months could have 5 depending on how a week is calculated) from the May subfolder? Are the names of the folders and files consistent or do they vary? Does the picture above represent the actual naming convention?
    – Rob D'Oria
    May 17, 2014 at 14:32
  • Could you post the server object model code you've tried...might give us a good starting point to work with.
    – Rob D'Oria
    May 21, 2014 at 16:24
  • They add new pdf any week. When ever they add a new pdf I have to get that and when I click a button in home page it has to open up.I have the following structure. Resources(Documentlibrary)/BW(folder)/ March 2014(folder)/week1 March 5 2014,Week2 March 12 2014 ,week3 March 19 2014,week1 March 26 2014. Resources(Documentlibrary)/BW(folder)/April 2014(folder)/week1 Apr2 2014,Week2 Apr9 2014
    – Sai
    May 21, 2014 at 16:26
  • No, I get the requirement, but instead of someone just giving you code it would be helpful for you to post the code you've already tried and let someone help you from there.
    – Rob D'Oria
    May 21, 2014 at 16:29
  • Hi Rob I added the code
    – Sai
    May 21, 2014 at 16:39

1 Answer 1

0

You can set the SPQuery to only search for a folder.

oWebsiteRoot.Lists.IncludeRootFolder = true;
SPList oList = oWebsiteRoot.Lists["Document_Library_Name"];
SPFolder oFolder = oList.RootFolder.SubFolders["Folder_Name"];
SPQuery oQuery = new SPQuery();
oQuery.Folder = oFolder;
SPListItemCollection collListItems = oList.GetItems(oQuery);
0

Your Answer

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

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