Last Accessed date of the SharePoint Site

ABHISHEK KUMAR 61 Reputation points
2023-09-23T02:26:28.3766667+00:00

Hi, I want to know how the last accessed date of the site is getting taken from the site. I am trying to take the last accessed date of the site in powershell script. There is no any direct response in both pnp and graph api... Everywhere I can see the lastitemModifiedDate.. Can you please let me know how I can take the last accessed date of the site?

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,773 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
3,191 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. ChengFeng - MSFT 5,040 Reputation points Microsoft Vendor
    2023-09-25T06:41:55.3533333+00:00

    Hi@ABHISHEK KUMAR

    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

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

    1. 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
    
    1. Connect to SharePoint Online Site
      Connect to your SharePoint Online site using PnP PowerShell. Replace '[https://your-sharepoint-site](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
    
    1. 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.OccurredWrite-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

    ***If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards

    Cheng Feng


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.