How to Find Old Content on Your OneDrive or SharePoint Site

Any organization has it’s own data lifecycle policy and for information stored in SharePoint there must be a retention period… let say 5 years. So your files modified more than 5 years ago are going to be deleted and you will not even notice it.

What if you want to know – which documents in your OneDrive or SharePoint site is older than some specific date – here are some options to find out.

Search with query parameters (GUI)

At any level of your site hierarchy – root level, library, folder etc. – you can refine your search results with, e.g. “LastModifiedTimeForRetention<2021-01-01” if you want to get all documents older than January 1st 2021.

In the screenshot below I use newer dates, but you got the idea:

If you need only Microsoft Word documents older than some specific date, you might use query: “*.docx LastModifiedTimeForRetention<2021-01-01”

If you need only Microsoft Word documents authored by some specific User and older than some specific date, you might use query:
“*.docx author:Patti LastModifiedTimeForRetention<2021-01-01”

Search with Graph API

The same query you can use to search content with Microsoft Graph API. Here is the code example:

$query = "LastModifiedTimeForRetention<2021-01-01"
$apiUrl = "https://graph.microsoft.com/beta/search/query"
$body = @"
{ 
  "requests": [
    {
      "entityTypes": [
        "driveItem"
      ],
      "query": {
        "queryString": "$query"
      }
    }
  ]
}
"@

$res = Invoke-RestMethod -Headers $Headers -Uri $apiUrl -Body $Body -Method Post -ContentType 'application/json'
$res.value[0].searchTerms
$res.value[0].hitsContainers[0].hits

Leave a Reply

Your email address will not be published. Required fields are marked *