SharePoint Online get list of WebParts using PowerShell

Jesper Hoegsdal 21 Reputation points
2020-12-11T00:54:52.877+00:00

In SharePoint Online I have create an out of the box site and added a couple of WebParts. I want to get the list of the WebParts and add them to other sites.

I'm using the command Get-PNPWebPart to get the WebParts from the site.

This is the code I have tried.

$user = "[username]"
$password = ConvertTo-SecureString "[password]" -AsPlainText -Force

$credentials = New-Object –TypeName "System.Management.Automation.PSCredential" –ArgumentList $user, $password

$siteURL = "https://[tenant].sharepoint.com/Sites/[site name]"

Connect-PnPOnline -Url $siteURL -Credentials $credentials

$serverRelativePageUrl = "/sites/[site name]/SitePages/Home.aspx"

$webparts = Get-PNPWebPart -ServerRelativePageUrl $serverRelativePageUrl

foreach($webpart in $webparts)
{
    Write-Host "WebPart title:" $webpart.WebPart.Title
}

When I run this I don't get any errors but the $webparts variable is empty.

Why is the $webParts variable empty when I try to get the list of WebParts from the SharePoint site?

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,301 questions
0 comments No comments
{count} votes

Accepted answer
  1. Baker Kong-MSFT 3,801 Reputation points
    2020-12-11T03:02:08.29+00:00

    Hi @Jesper Hoegsdal ,

    Per my experience, Get-PnPWebPart can only get webpart from classical page. it's not suitable for modern page. You can use below cmdlets to get webpart on modern page:

    Get-PnPClientSideComponent -Page 'Home'

    47263-image.png

    or: https://www.c-sharpcorner.com/blogs/add-remove-and-get-all-webparts-from-modern-site-page-using-pnp-powershell

    $page= Get-PnPClientSidePage -Identity "Home.aspx"  
    $webParts = $page.Controls    
    #if there are more than one webparts    
    foreach($webpart in $webparts) {    
        Write - Host "WebPart Id "    
        $webpart.InstanceId    
        Write - Host "Title "    
        $webpart.Title    
    }    
    

    Note: If you get empty output, please edit the Home page and republish it.
    47125-image.png

    Thanks
    Baker Kong

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Baker Kong-MSFT 3,801 Reputation points
    2020-12-11T02:05:12.067+00:00

    Hi @JesperHoegsdalCloudFixers-4976,

    Get-PnPWebPart Returns all webparts defined on the given page. Since your home page (/SitePages/Home.aspx) does not have added any web part, it will output an empty variable.

    If you want to get installed webpart in the site, you may consider using following cmdlets:

    Get-PnPApp  
    Get-PnPApp|Where-Object {$_.InstalledVersion -ne $null}  
    

    47163-image.png
    47213-image.png

    Or:

    Get-PnPAppInstance  
    

    References:

    Best Regards,
    Baker Kong


    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. Jesper Hoegsdal 21 Reputation points
    2020-12-11T03:13:26.2+00:00

    Answer here: 65245172

    The problem was that the code I used only works on SharePoint Classic sites and I'm using a Modern site.

    This is the solution.

    $user = "user@tenant.onmicrosoft.com"
    $password = ConvertTo-SecureString "password" -AsPlainText -Force
    
    $credentials = New-Object –TypeName "System.Management.Automation.PSCredential" –ArgumentList $user, $password
    
    $siteURL = "https://tenant.sharepoint.com/sites/JerryModernTeam"
    
    Connect-PnPOnline -Url $siteURL -Credentials $credentials
    
    
    $page=Get-PnPClientSidePage -Identity "Home.aspx"
    
    $webParts = $page.Controls  
    #if there are more than one webparts  
    foreach($webpart in $webParts) {  
        Write - Host "WebPart Id "  
        $webpart.InstanceId  
        Write - Host "Title "  
        $webpart.Title  
    }  
    
    0 comments No comments