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
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
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 Yellow
You 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- kalpeshvaghelaSep 06, 2022Iron Contributor
If you want to find out document using OR condition then below should be query which will return all the document where "Keyword 1" and "Keyword 2" word is there in content and also return all the excel document irrespective of content of the document
$keyword="(('Keyword 1') OR ('Keyword 2') OR (FileExtension:'xslx')) PATH:$SiteUrl"If you want to find out document using AND condition then below should be query which will return only excel document where "Keyword 1" and "Keyword 2" word is there in content
$keyword="(('Keyword 1') OR ('Keyword 2')) AND (PATH:$SiteUrl) AND (FileExtension=xlsx)"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 ContributorThanks.
Last question I asked earlier also
Is there any url or article, where I can see what else property are available to add in search result or where I can learn to pass keyword ?
Avian