2

I have an Excel file that is in a file library. I'd like to have a page that shows when that Excel file was last modified.

Anybody know of a piece of code that will show "Date Modified" of an Excel file in a file library?

3 Answers 3

2

You can use below code on page or user control.

public DateTime GetLastModifedForFile()
{ 

 SPSecurity.RunWithElevatedPrivileges(delegate()
 {
  using (SPSite siteColl = new SPSite("http://spsite"))
  {
   using (SPWeb web = siteColl.OpenWeb())
   {

     SPFile spfile = web.GetFile("Source_Folder_Name/Source_File");
     if (spfile.Exists)
     {
        return spfile.TimeLastModified;
     }

   } 
  }
 });

    return null;
}
4
  • I'm new to Sharepoint coding... where do I put this on my page? (meaning: how do I incorporate this?)
    – Mr_Thomas
    Commented Nov 16, 2011 at 16:38
  • This link can help you to start with : msdn.microsoft.com/en-us/library/ee330921.aspx Commented Nov 16, 2011 at 16:52
  • What I ended up doing was inserting a Data View on my page. That allowed me to get what I needed. Thank you, Amit, for sending me down the right road.
    – Mr_Thomas
    Commented Nov 16, 2011 at 20:11
  • Dataview will work only if user have permission on library.If not (like you mentioned in your coment in another answer), you have to use SPSecurity.RunWithElevatedPrivileges Commented Nov 17, 2011 at 2:38
2

The Modified column displays this automatically. Be sure to include the column in your view.

5
  • I see the Modified date when I look at the File Library. I want that date to appear on a page in the site for those that don't have access to look at the 'raw' file library.
    – Mr_Thomas
    Commented Nov 16, 2011 at 15:56
  • How are you exposing the library to end users? Are you putting a web part on the publishing page? If so, include the modified date in that view. Commented Nov 16, 2011 at 16:14
  • I just have an aspx page with some HTML links to the file where they can download the file.
    – Mr_Thomas
    Commented Nov 16, 2011 at 16:38
  • Are you dynamically generating that content? The easiest approach is to add a web part into a web part zone onto your publishing page and expose the desired metadata. Other options include writing some javascript/jQuery to fetch the information using a handy tool called SPServices, spservices.codeplex.com. Commented Nov 16, 2011 at 16:43
  • The Excel file is uploaded once every two weeks. It's always called the same thing ("Deposit.xls"). When the user hits the site page, there's a link there that allows them to download the file. I'll look at that SPServices tool. I've seen that listed before as helping in certain situations.
    – Mr_Thomas
    Commented Nov 16, 2011 at 16:49
0

I achieved this by using document webpart, and modifying the current view to show only name and modified and filtered it with the file name, disable tool bar etc as per your need. Amit's solution is the best one, but I had to use this because inside our company environment we are not allowed to add any scripts or can we use share-point designer.

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.