How to 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:
PnP PowerShell to Search SharePoint Online site:
Get All Excel Files in the Site Collection and Export Search Results to CSV
Let's search for all Excel files from a SharePoint Online site and export search results to a CSV file.
Please note, this approach queries files from search indexed, but there could be a lag in indexing schedules index may not real time. Here is a reference on Submit-PnPSearchQuery
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://crescent.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 site:
#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 } $ResultsTo get All available properties of the search results, use: $SearchResults.ResultRows[0] that will get you all properties of the search results:
- Author
- CollapsingStatus
- contentclass
- ContentTypeId
- Culture
- deeplinks
- Description
- DisplayAuthor
- docaclmeta
- DocId
- EditorOWSUSER
- FileExtension
- FileType
- GeoLocationSource
- HitHighlightedProperties
- HitHighlightedSummary
- importance
- IndexDocId
- IsContainer
- IsDocument
- IsExternalContent
- LastModifiedTime
- LinkingUrl
- OriginalPath
- ParentLink
- PartitionId
- Path
- PictureThumbnailURL
- piSearchResultId
- ProgId
- RenderTemplateId
- ResultTypeId
- ResultTypeIdList
- SecondaryFileExtension
- SectionIndexes
- SectionNames
- ServerRedirectedEmbedURL
- ServerRedirectedPreviewURL
- ServerRedirectedURL
- SiteDescription
- SiteId
- SiteLogo
- SiteName
- Size
- SPWebUrl
- Title
- UniqueId
- UrlZone
- ViewsLifeTime
- ViewsRecent
- WebId
- WebTemplate
- WorkId
- Write
Get All Excel Files in the Site Collection and Export Search Results to CSV
Let's search for all Excel files from a SharePoint Online site and export search results to a CSV file.
#Parameters $SiteUrl = "https://crescent.sharepoint.com/sites/Marketing" $SearchQuery = "*.xlsx Path:" + $SiteURL $CSVFile = "C:\Temp\ExcelFiles.csv" #Connect to PnP Online Connect-PnPOnline -Url $SiteUrl -UseWebLogin #Execute Search $SearchResults = Submit-PnPSearchQuery -Query $SearchQuery -All -TrimDuplicates $False -SelectProperties Filename, Author, Size, ListItemID, LastModifiedTime #Collect Data from search results $Results = @() ForEach ($ResultRow in $SearchResults.ResultRows) { $Results += [pscustomobject] @{ Filename = $ResultRow["Filename"] Author = $ResultRow["Author"] Size = $ResultRow["Size"] LastModified = $ResultRow["LastModifiedTime"] ListItemID = $ResultRow["ListItemID"] ParentFolder = $ResultRow["ParentLink"] URL = $ResultRow["Path"] } } $Results #Export results to CSV $Results | Export-Csv -Path $CSVFile -NoTypeInformationThis script searches the given search query and exports search results to a CSV file.
Please note, this approach queries files from search indexed, but there could be a lag in indexing schedules index may not real time. Here is a reference on Submit-PnPSearchQuery
I am trying to do the same thing but i connecting via AppID. I get access denied when i run the search cmdlet. Any possible solution for this?
ReplyDeleteThe APPId must have permissions granted to the sites! Double check if your AppID has permissions to the sites. How to use Connect-PnPOnline with AppID and AppSecret in SharePoint Online
DeleteThank you for your valuable information. It was very helpful.
ReplyDeleteOne comment on this is that it would be better to add encoding option when downloading to Excel like this to avoid weird characters in case of using Asian characters.
$Results | Export-Csv -Path $CSVFile -NoTypeInformation -Encoding Default