PnP powershell scripting to fetch all the content query webparts available under SharePoint page library

Asuri Spandana 20 Reputation points
2024-01-11T09:27:46.4833333+00:00

Hi Team, I want to get a detail report in which all pages the content query webpart available in SharePoint 2016 site pages using PnP powershell scripting

SharePoint Server
SharePoint Server
A family of Microsoft on-premises document management and storage systems.
2,222 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,068 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Emily Du-MSFT 41,786 Reputation points Microsoft Vendor
    2024-01-12T03:05:32.0066667+00:00

    Please use following PowerShell to list generate content query web part in used in a site collection.

    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
     
    #Configuration parameters
    $SiteURL = "http://webapplication/sites/test"
    $ReportOutput="C:\webpart.csv"
     
    $ResultCollection = @()
     
    #Get All Subsites in a site collection and iterate through each
    $Site = Get-SPSite $SiteURL
    ForEach($Web in $Site.AllWebs)
    {
        write-host Processing $Web.URL
        # If the Current Web is Publishing Web
        if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($Web))
        {
            #Get the Publishing Web
            $PubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($Web)
                       
            #Get the Pages Library
            $PagesLib = $PubWeb.PagesList
         }
         else
         {
            $PagesLib = $Web.Lists["Site Pages"]
         }            
            #Iterate through all Pages 
            foreach ($Page in $PagesLib.Items | Where-Object {$_.Name -match ".aspx"})
            {
                $PageURL=$web.site.Url+"/"+$Page.File.URL
                $WebPartManager = $Page.File.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
                     
                #Get All Web Parts data
                foreach ($WebPart in $WebPartManager.WebParts| Where-Object { $_.GetType().ToString() -like "*ContentByQuery*"})
                {
                    $Result = New-Object PSObject
                    $Result | Add-Member -type NoteProperty -name "Site URL" -value $web.Url
                    $Result | Add-Member -type NoteProperty -name "Page URL" -value $PageURL
                    $Result | Add-Member -type NoteProperty -name "Web Part Title" -value $WebPart.Title
                    $Result | Add-Member -type NoteProperty -name "Web Part Type" -value $WebPart.GetType().ToString()
     
                    $ResultCollection += $Result
                }
            }
    }
    #Export results to CSV
    $ResultCollection | Export-csv $ReportOutput -notypeinformation
    

    Result:

    enter image description here


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.