Forum Discussion

SimSim8791's avatar
SimSim8791
Copper Contributor
Jul 21, 2023

Last consultation date of a file in sharepoint online site

Hello 

 

I'm looking for the way to show the last consultation date of a file in a sharepoint site.

 

This is to help me to know which files are not use at all anymore and should be archived or deleted depending of the importance of this file. 

I can see the last modification date, but this is not relevant as i can have a 5 years old process still consult every day by lot of users.

 

Thanks for your help 

Simon

3 Replies

  • FirdouzHussain's avatar
    FirdouzHussain
    Copper Contributor
    Hi SimSim8791

    Are you able achieve the functionality? I have Same kind of requirement. Please let me know the process you have followed.
  • LeonPavesic's avatar
    LeonPavesic
    Silver Contributor

    Hi SimSim8791,

    You can do this by using these options

    1. Enable Auditing:
    To start tracking file access events and find the last consultation date, you'll need SharePoint administrator or global administrator permissions. Follow these steps:
    - Go to your SharePoint Online site and click the "Settings" gear icon in the top right corner.
    - Choose "Site settings" from the menu.
    - Under "Site Collection Administration," click on "Site collection audit settings."
    - Enable the "Opening or downloading documents, viewing items in lists, or viewing item properties" option under "Documents and Items."

    Configure audit data for a site collection - Microsoft Support

    2. Wait for Auditing Data:

    Once you've enabled auditing, SharePoint Online will start collecting audit data. Keep in mind that it might take some time before the audit data becomes available for use.


    3. Use PowerShell to Get Audit Data:
    you can use PowerShell along with the SharePoint PnP module to retrieve the last accessed date of files. If you haven't installed the SharePoint PnP module, you can do it with this simple command:

    Install-Module SharePointPnPPowerShellOnline -Force

    4. Connect to SharePoint Online Site
    Connect to your SharePoint Online site using PnP PowerShell. Replace `'https://your-sharepoint-site'` with your actual SharePoint site URL. You'll be prompted to provide your credentials:

    Connect-PnPOnline -Url "https://your-sharepoint-site" -UseWebLogin

    5. Retrieve Last Consultation Dates:
    retrieve the audit log data and filter for file access events to get the last consultation date for each file in your chosen library. Make sure to replace `'/sites/YourSite/LibraryName/'` with the correct path to the document library you want to track:

    $libraryUrl = '/sites/YourSite/LibraryName/'
    $auditData = Get-PnPAuditLog -Query "<Query><Where><And><Eq><FieldRef Name='FileDirRef'/><Value Type='Text'>$libraryUrl</Value></Eq><Eq><FieldRef Name='Event' /><Value Type='String'>View</Value></Eq></And></Where></Query>"

    foreach ($entry in $auditData) {
    $fileUrl = $entry.ItemUrl
    $lastAccessedDate = $entry.Occurred
    Write-Host "File URL: $fileUrl, Last Consultation Date: $lastAccessedDate"
    }

    When the First part of the script doesn't work, install SharePoint Online Management Shell, and run Powershell commands as an admin in SharePoint Online Management Shell.

    Download SharePoint Online Management Shell from Official Microsoft Download Center



    Please click Mark as Best Response & Like if my post helped you to solve your issue.
    This will help others to find the correct solution easily. It also closes the item.


    If the post was useful in other ways, please consider giving it Like.


    Kindest regards,


    Leon Pavesic

    • FirdouzHussain's avatar
      FirdouzHussain
      Copper Contributor
      Hi,

      I am not able to find Get-PnPAuditLog command.Can you please provide the reference of it.

Resources