0

I have on prime SharePoint 2016 site which has couple of web part on the homepage.I want to read the web part properties from the page and Export the data to CSV file using Power Shell commands.

2 Answers 2

1

You can use the below PnP command :

Get-PnPWebPartProperty -ServerRelativePageUrl /sites/demo/sitepages/home.aspx -Identity ccd2c98a-c9ae-483b-ae72-19992d583914 | Export-csv "file path" 

Notes:

  • In the above, you just can pass your home page URL from where you want to extract the web part property.

Reference :

Get-PnPWebPartProperty

Using PowerShell CSOM:

Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"  
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Publishing.dll" 


$siteURL = ""  
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)

#Not required for on premise site - Start  
$userId = ""  
$pwd = Read-Host -Prompt "Enter password" -AsSecureString  
$creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)  
$ctx.credentials = $creds   
#Not required for on premise site - End

#page to be viewed  
$pageName = "TestPage1.aspx"  
#Get the page  
$file = $ctx.Web.GetFileByServerRelativeUrl("/Pages/TestPage1.aspx")  
$ctx.Load($file)  
$ctx.ExecuteQuery()

#Get all the webparts  
Write-Host "Retrieving webparts"  
$wpManager = $file.GetLimitedWebPartManager([Microsoft.SharePoint.Client.WebParts.PersonalizationScope]::Shared)  
$webparts = $wpManager.Webparts  
$ctx.Load($webparts)  
$ctx.ExecuteQuery()



if($webparts.Count -gt 0){  
    Write-Host "Looping through all webparts"  
    foreach($webpart in $webparts){  
        $ctx.Load($webpart.WebPart.Properties)  
        $ctx.ExecuteQuery()  
        $propValues = $webpart.WebPart.Properties.FieldValues  
        Write-Host "ID: $webpart.id"  
        foreach($property in $propValues){  
            Write-Host "Title: " $property.Title                  
            Write-Host "Description: " $property.Description  
            Write-Host "Chrome Type: " $property.ChromeType  
        }  

    }  
}  

Reference :

Retrieve Webparts From Page Using CSOM With PowerShell On SharePoint

How to export the report into the CSV file?

if($webparts.Count -gt 0){  
    Write-Host "Looping through all webparts"  
    foreach($webpart in $webparts)
      {  
        $ctx.Load($webpart.WebPart.Properties)  
        $ctx.ExecuteQuery()  
        $propValues = $webpart.WebPart.Properties.FieldValues  
        Write-Host "ID: $webpart.id"  
        foreach($property in $propValues){  
            Write-Host "Title: " $property.Title                  
            Write-Host "Description: " $property.Description  
            Write-Host "Chrome Type: " $property.ChromeType

             $listItemData = @{
            "Webpart Title" = $property.Title
            "Webpart Description" = $property.Description
            "Chrome Type" = $property.ChromeType 
            }
           New-Object PSObject -Property $listItemData

        }  

    }  
}  

$listItemData |Export-Csv -NoTypeInformation -Path "Your CSV file path"

To export in CSV refer to the below article:

Get document library inventory report in SharePoint using PowerShell script

7
  • Now i am able to connect the site.. new error came..
    – Liz
    Commented Jun 2, 2020 at 10:51
  • PS C:\> Write-Host " Current web title is '$($web.Title)', $($web.Url)" Current web title is '', PS C:\> $pageName = "Test.aspx" PS C:\> $file = $ctx.Web.GetFileByServerRelativeUrl("/SitePages/Test.aspx") >> PS C:\> >> $ctx.Load($file) >> PS C:\> $ctx.ExecuteQuery() Exception calling "ExecuteQuery" with "0" argument(s): "Value does not fall within the expected range." At line:1 char:1 + $ctx.ExecuteQuery() + ~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ServerException
    – Liz
    Commented Jun 2, 2020 at 10:55
  • 1
    Pass the page URL like : "/sites/yoursitename/sitepages or Pages library/YourPage.aspx"
    – SP 2022
    Commented Jun 2, 2020 at 11:25
  • yes it is working thank you so much
    – Liz
    Commented Jun 2, 2020 at 11:54
  • Is there is way to export this web parties to CSV file
    – Liz
    Commented Jun 2, 2020 at 11:55
0

enter image description here

After exporting i am getting like this.. But this is not correct way.

Chrome Type: 0

Webpart Title           Chrome Type Webpart Description
-------------           ----------- -------------------
Web Part Page Title Bar           2
Content Editor                    0 Allows authors to enter rich text content.
Script Editor                     2 Allows authors to insert HTML snippets or scripts.
Image Viewer                      0 Displays a specified image.


PS C:\>
PS C:\> $listItemData |Export-Csv -NoTypeInformation -Path "C:\Users\`enter code here`"
PS C:\>
2
  • But script is working fine.. the exporting is not working as expected
    – Liz
    Commented Jun 2, 2020 at 14:31
  • question raised.. please some one help me
    – Liz
    Commented Jun 3, 2020 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.