Forum Discussion
Keyword Search in SharePoint 2016/Online
- Sep 06, 2022
Here is the updated script:
$SiteURL ="https://contoso.sharepoint.com/sites/KeywordSearch1/" $keyword="(('Keyword 1') OR ('Keyword 2')) PATH:$SiteUrl" $currentTime=$(get-date).ToString("yyyyMMddHH"); $outputFilePath="D:\Kalpesh\Scripts\results-"+$currentTime+".csv" ## Connect to SharePoint Online site Connect-PnPOnline -Url $SiteURL -UseWebLogin Write-Host "Site connected sucessfully" -ForegroundColor Green ## Executes an arbitrary search query against the SharePoint search #$results=Submit-PnPSearchQuery -Query $keyword -All -TrimDuplicates $False // here it will export 10 items in search result $results = Submit-PnPSearchQuery -Query $keyword -All -TrimDuplicates:$false -SelectProperties @("Path","SPWebUrl","UrlDepth","Filename") ## Get the results in the hash table $hashTable=@() foreach($resultRow in $results.ResultRows) { $obj=New-Object PSObject $resultRow.GetEnumerator()| ForEach-Object{ $obj | Add-Member Noteproperty $_.Key $_.Value} $hashTable+=$obj; $obj=$null; } ## Export to CSV $hashtable | export-csv $outputFilePath -NoTypeInformation -Force Write-Host "Result exported sucessfully" -ForegroundColor YellowYou can use multiple key word in query like "(("Keyword 1") OR ("Keyword 2")) and so on". Reference link: https://social.msdn.microsoft.com/Forums/office/en-US/da8ffae4-e715-4a35-bdc7-5c598ac1e057/how-can-i-search-multiple-keywords-with-space-in-sharepoint-2010-?forum=sharepointdevelopmentprevious
Even if we provide SelectedProperties in command, few fixed column's value will return anyway e.g. DocId, Rank, SiteId. You can update your loop where you are preparing hash table to remove those columns from csv.
Regarding Document Library path, as per my knowledge there no managed property or crawled property which can give document library path, here you need to do some string operation using UrlDepth (which is the depth of the URL i.e. split URL by "/") and SPWebUrl values.
Hope it will helpful to you and if so then Please mark my response as Best Response & Like to help others in this community
You need to include one more condition in your query to trim result based on site URL i.e. PATH:{URL}
Try below code:
$SiteURL ="https://domain.sharepoint.com/sites/test"
$keyword="microsoft.com PATH:$SiteUrl"
$currentTime=$(get-date).ToString("yyyyMMddHHmmss");
$outputFilePath="D:\Fix\results-"+$currentTime+".csv"
## Connect to SharePoint Online site
Connect-PnPOnline -Url $SiteURL -UseWebLogin
Write-Host "Site connected sucessfully" -ForegroundColor Green
## Executes an arbitrary search query against the SharePoint search
#$results=Submit-PnPSearchQuery -Query $keyword -All -TrimDuplicates $False // here it will export 10 items in search result
$results = Submit-PnPSearchQuery -Query $keyword -MaxResults 10
## Get the results in the hash table
$hashTable=@()
foreach($resultRow in $results.ResultRows)
{
$obj=New-Object PSObject
$resultRow.GetEnumerator()| ForEach-Object{ $obj | Add-Member Noteproperty $_.Key $_.Value}
$hashTable+=$obj;
$obj=$null;
}
## Export to CSV
$hashtable | export-csv $outputFilePath -NoTypeInformation
Write-Host "Result exported sucessfully" -ForegroundColor Yellow
Hope it will helpful to you and if so then Please mark my response as Best Response & Like to help others in this community
- Avian 1Sep 05, 2022Iron Contributor
Hello Kalpeshvaghela
Thank you very much!!
It is working for filename, but not working for contents. I want to do search in Word and Excel files.
Secondly I need only SharePoint Site, DOcumentlibrary path and filename.I also noticed that If I used following , it is throwing error.
$results=Submit-PnPSearchQuery -Query $keyword -All -TrimDuplicates $False
How to search mutiple keyword in excel or word documents?
Avian- kalpeshvaghelaSep 06, 2022Iron Contributor
Here is the updated script:
$SiteURL ="https://contoso.sharepoint.com/sites/KeywordSearch1/" $keyword="(('Keyword 1') OR ('Keyword 2')) PATH:$SiteUrl" $currentTime=$(get-date).ToString("yyyyMMddHH"); $outputFilePath="D:\Kalpesh\Scripts\results-"+$currentTime+".csv" ## Connect to SharePoint Online site Connect-PnPOnline -Url $SiteURL -UseWebLogin Write-Host "Site connected sucessfully" -ForegroundColor Green ## Executes an arbitrary search query against the SharePoint search #$results=Submit-PnPSearchQuery -Query $keyword -All -TrimDuplicates $False // here it will export 10 items in search result $results = Submit-PnPSearchQuery -Query $keyword -All -TrimDuplicates:$false -SelectProperties @("Path","SPWebUrl","UrlDepth","Filename") ## Get the results in the hash table $hashTable=@() foreach($resultRow in $results.ResultRows) { $obj=New-Object PSObject $resultRow.GetEnumerator()| ForEach-Object{ $obj | Add-Member Noteproperty $_.Key $_.Value} $hashTable+=$obj; $obj=$null; } ## Export to CSV $hashtable | export-csv $outputFilePath -NoTypeInformation -Force Write-Host "Result exported sucessfully" -ForegroundColor YellowYou can use multiple key word in query like "(("Keyword 1") OR ("Keyword 2")) and so on". Reference link: https://social.msdn.microsoft.com/Forums/office/en-US/da8ffae4-e715-4a35-bdc7-5c598ac1e057/how-can-i-search-multiple-keywords-with-space-in-sharepoint-2010-?forum=sharepointdevelopmentprevious
Even if we provide SelectedProperties in command, few fixed column's value will return anyway e.g. DocId, Rank, SiteId. You can update your loop where you are preparing hash table to remove those columns from csv.
Regarding Document Library path, as per my knowledge there no managed property or crawled property which can give document library path, here you need to do some string operation using UrlDepth (which is the depth of the URL i.e. split URL by "/") and SPWebUrl values.
Hope it will helpful to you and if so then Please mark my response as Best Response & Like to help others in this community
- Avian 1Sep 06, 2022Iron ContributorHello Kalpeshvaghela,
Thank you very much. It is working like charm.
I also tried to search like below to filter on *.xlxs with some keyword, it is not working. AM I missing anything
$keyword="(('microsoft') OR ('*.xlsx')) PATH:$SiteUrl"
Is there any url, where I can see what else property are available to add in search result
Regards
Avian