question

prajaktaubare-8568 avatar image
0 Votes"
prajaktaubare-8568 asked prajaktaubare-8568 edited

Pull a csv report consisting of SharePoint Online sites/libraries/content basis certain keyword.

Objective is to identify SharePoint online content having mention of certain keywords.

Will pnp powershell help in fetching a report basis following conditions:

Loop through all site collection and subsites in a tenant.
Look for certain keywords mentioned.
Out put should have: site, owner, last modified, file name, file path.

Search/advanced search might help here, however we need data to refer/analyze hence a csv report.

office-sharepoint-onlinewindows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

EmilyDu-MSFT avatar image
0 Votes"
EmilyDu-MSFT answered prajaktaubare-8568 edited

@prajaktaubare-8568

You could use following PowerShell to search documents using Keyword Query.

Note: You should use your own $SearchQuery、$SiteURL、$CSVFile in the PowerShell.

 #Load SharePoint CSOM Assemblies
 Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
 Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Search.dll"
     
 #Config Variables
 $SiteURL="https://tenant.sharepoint.com/"
 $SearchQuery= "path:https://tenant.sharepoint.com AND IsDocument:true AND (NOT FileType:aspx) AND Title:test"
 $CSVFile = "C:\SearchResults.csv"
     
 Try {
     $Cred= Get-Credential
     $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
      
     #Setup the context
     $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
     $Ctx.Credentials = $Credentials
         
     #Define Keyword
     $KeywordQuery = New-Object Microsoft.SharePoint.Client.Search.Query.KeywordQuery($Ctx)
     $KeywordQuery.QueryText = $SearchQuery
     $KeywordQuery.RowLimit  = 500
     $keywordQuery.SelectProperties.Add("CreatedBy")
     $keywordQuery.SelectProperties.Add("LastModifiedTime")
     $keywordQuery.SortList.Add("LastModifiedTime","Asc")
     
     #Execute Search        
     $SearchExecutor = New-Object Microsoft.SharePoint.Client.Search.Query.SearchExecutor($Ctx)
     $SearchResults = $SearchExecutor.ExecuteQuery($KeywordQuery)
     $Ctx.ExecuteQuery()
     
     Write-host "Search Results Found:"$SearchResults.Value[0].ResultRows.Count
     
     #Get Search Results
     If($SearchResults)
     {
         $Results = @()
         foreach($Result in $SearchResults.Value[0].ResultRows)
         {
             $Results += New-Object PSObject -Property @{
                         'Document Name' =  $Result["Title"]
                         'URL' = $Result["Path"]
                         'Created By' = $Result["CreatedBy"]  
                         'Last Modified' = $Result["LastModifiedTime"]               
                         }
         }
         $Results
         #Export search results to CSV
         $Results | Export-Csv $CSVFile -NoTypeInformation
         Write-Host -f Green "Search Results Exported to CSV File!"
     }
 }
 Catch {
     write-host -f Red "Error Getting Search Results!" $_.Exception.Message
 }

If an Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.




· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@prajaktaubare-8568

Do you have any progress on this issue?

Please remember to update this thread if you need further assistance.

0 Votes 0 ·


@EmilyDu-MSFT

·
Thanks. This was helpful and I am getting results too.
However I wanted to add one more condition here.
In the search results I don want results from a particular site.

$SearchResults=Submit-PnPSearchQuery -Query $keyword | Where-Object {$_.SiteName -notcontains "URL of the unwanted site"}

This is not working.

0 Votes 0 ·