Office 365/Sharepoint Online - PowerShell Script To Call Search API And Get The Result

In this article, I will explain using KeywordQuery class from PowerShell script and writing results to .CSV files. I will also explain the scenario where we need to use Search from the PowerShell script.

Our client has an Office 365 tenant and is very heavily using SharePoint online. We have one big SharePoint online site collection. Site collection has lots of content and there is also unwanted content available. The customer wants to clean up the site collection. Customer requested to get all the items/documents which are not updated since last year. Customer also wants the view count / open count for respective list items and documents.

One thought came to mind, that is - Audit Reports which we were supposed to use in the On Premises version. So we went to Site Settings >> Site Collection Administration >> Audit log reports (https://myTenant/_layouts/15/Reporting.aspx?Category=Auditing)

But in SharePoint online Audit log reports don't works, it says, even though Auditing is enabled.

Figure 1 : SharePoint Online : Audit Log Report

These audit reports never worked for us in SharePoint online. So, the option remains of using Search and managing meta data property “ViewsLifeTime” and “ViewsLifeTimeUniqueUsers”. We configured the Content Search webpart and could see the result for some data. For some data these properties didn’t return any result.

Our customer wants the result in excel sheet and its really difficult to get content search Web Part result in excel sheet though we can write from display template.

Finally, we decided to write the PowerShell script and call the search APIs like KeywordQuery from the PowerShell script.