Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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 simple...how 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
add comment

4 Answers

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
add comment

(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
add comment

Try this:

// Run with an account with higher privileges than the current user 
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        using (SPSite oSite = new SPSite("http://semallsolution.com.my"))
        {
            using (SPWeb oWeb = oSite.OpenWeb())
            {
                // Turn off security validation 
                oWeb.AllowUnsafeUpdates = true;

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

                // 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;
                grdRecent.DataBind();

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

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
add comment

Your Answer

 
discard

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.