SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a document library that a user uploads a file to daily. I then have to display the data from the most recently uploaded document (xls) in a web part.

I have gotten everything working except figuring out which file is the latest to pull the data from. I know there is a list.LastItemModifiedDate to get the latest modified date of the list/library.

I may have been looking at this issue for too long and missing something real do I then get the title/filename of that most recently uploaded document?

Thanks, Ninel

share|improve this question
Have you thought about using the content query web part for a no code solution? – Tom Resing Jul 8 '11 at 17:19

Another no code option it to create a view of the document library that is limited to 1 result and is sorted by either created date in descending order or by modified (just in case modified counts as well as James indicated). Then you can put the list web part on the page, and set it to that view.

share|improve this answer

(Assuming you're developing a web part in code) - you could issue an SPQuery to the library, sorted by Modified descending, and get the first result.

share|improve this answer
I am writing a web part. Can you please help me with the SPQuery? – ninel Jul 8 '11 at 17:28
SharePoint Malaya's code sample shows how to do this with SPQuery. – James Love Jul 8 '11 at 17:29

Try this:

// Run with an account with higher privileges than the current user 
        using (SPSite oSite = new SPSite(""))
            using (SPWeb oWeb = oSite.OpenWeb())
                // Turn off security validation 
                oWeb.AllowUnsafeUpdates = true;

                // Construct CAML query
                StringBuilder query = new StringBuilder();
                query.Append(@"<FieldRef Name='Modified' Ascending='False' />");

                // Instantiate SPQuery
                SPQuery oQuery = new SPQuery();
                oQuery.Query = query.ToString();

                // Execute CAML query
                SPList oList = oWeb.Lists["Shared Documents"];
                SPListItemCollection oListItems = oList.GetItems(oQuery);
                DataTable data = oListItems.GetDataTable();

                // Bind to spgridview
                grdRecent.DataSource = data.DefaultView;

                // Turn on security validation 
                oWeb.AllowUnsafeUpdates = false;
share|improve this answer
I think this would do it. I'd plus one if you change the RunWithElevatedPrivileges call to a SPUserToken call. See… for more detail on how and why. – Tom Resing Jul 8 '11 at 17:18

A content by query webpart with custom styling might be an option too. This doesn't require a solution deployment, which will make your administrator happy ;)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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