SOLVED

Keyword Search in SharePoint 2016/Online

Iron Contributor

Hello,

 

I want to key word search in office document in SharePoint 2016 or SharePoint Online.

I tried following code, but it is searching from all the site collections instead of site which I give them in $SiteUrl variable in below. 

$SiteURL ="https://domain.sharepoint.com/sites/test"

$keyword="microsoft.com"

$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

 

This code search properly microsoft.com in all the sites, but I wan to search in specific site.

 Is there any alternative to do search keyword in  particular site, My preference to search in SP2016 if possible?

 

Regards

Avian

 

7 Replies

@Avian 1 

 

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

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

best response confirmed by Avian 1 (Iron Contributor)
Solution

@Avian 1 

 

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... 

 

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 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

@Avian 1 

 

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

 

Thanks.

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

@Avian 1 

 

You can go to SharePoint Admin Center using URL https://{Tenant}-admin.sharepoint.com and click on More Features and Click on 

 

kalpeshvaghela_0-1662554142268.png

 

Now Click on Search Schema which will redirect to the page where all the available managed properties are shown you can even search here.

 

kalpeshvaghela_1-1662554207846.png

 


Hope it will helpful to you and if so then Please mark my response as Best Response & Like to help others in this community

 

 

1 best response

Accepted Solutions
best response confirmed by Avian 1 (Iron Contributor)
Solution

@Avian 1 

 

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... 

 

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

 

View solution in original post