Search SharePoint Online using PnP PowerShell

Requirement: Run Search Query in SharePoint Online using PowerShell

How to Search SharePoint Online using PowerShell?
Using Keyword query to execute search and export results to CSV with CSOM is explained in my other post: How to Search SharePoint Online using PowerShell-Keyword Query?, Now, I need to get the List Item ID of the latest item created in a SharePoint Online list:
#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/DocHub"
$ListURL= "https://actisportal.sharepoint.com/sites/DocHub/Documents"
$SearchQuery = "Path:" + $ListURL
 
#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin

#Perform Search     
$SearchResults = Submit-PnPSearchQuery -Query $SearchQuery -All -SelectProperties ListItemID -SortList @{Created="Descending"}
 
#Get the Last Created Item's ID
Write-host "The Latest Item ID is:" $SearchResults.ResultRows[0]["ListItemID"]

PnP PowerShell to Search SharePoint Online:
#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/DocHub"
$SearchQuery = "Title:document* Path:" + $SiteURL

#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin

#Run Search Query     
$SearchResults = Submit-PnPSearchQuery -Query $SearchQuery -All

$Results = @()
foreach($ResultRow in $SearchResults.ResultRows)  
{  
    #Get All Properties from search results
    $Result = New-Object PSObject  
    $ResultRow.GetEnumerator()| ForEach-Object { $Result | Add-Member Noteproperty $_.Key $_.Value}  
    $Results+=$Result
}
$Results

Here is a reference on Submit-PnPSearchQuery


Search SharePoint Online using PnP PowerShell Search SharePoint Online using PnP PowerShell Reviewed by Salaudeen Rajack on March 01, 2019 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.