0

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

1
  • Have you thought about using the content query web part for a no code solution?
    – Tom Resing
    Jul 8, 2011 at 17:19

4 Answers 4

2

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.

1

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

2
  • I am writing a web part. Can you please help me with the SPQuery?
    – ninel
    Jul 8, 2011 at 17:28
  • SharePoint Malaya's code sample shows how to do this with SPQuery.
    – James Love
    Jul 8, 2011 at 17:29
1

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;
            }
        }
    });
1
1

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 ;)

Your Answer

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

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