0

I am able to read the SharePoint web part properties and values using below code and able to export it. But after exporting, when i open the file could not find exact values.

Find the code below:

$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 

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

Output of code:

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.

Exported CSV:

enter image description here

why i am not able to get the output values??

2 Answers 2

1

I have updated your code and it works fine now.

$siteURL="https://contoso.sharepoint.com/sites/dev"
$userId = "[email protected]"  
$pwd = Read-Host -Prompt "Enter password" -AsSecureString 
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
$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("/sites/dev/SitePages/webpartpage.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()


$listItemData=@()
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 += New-Object PSObject -Property @{
                                "Webpart Title" = $property.Title
"Webpart Description" = $property.Description                
"Chrome Type" = $property.ChromeType                                                             
            }


        }  

    }  
}  

$listItemData |Export-Csv -NoTypeInformation -Path "C:\\Temp\\test.csv"

Test Result:

enter image description here

1
  • Thank you.. you saved my day
    – Liz
    Commented Jun 4, 2020 at 11:00
1

You can use the code this way - it is working fine.

Function GetWebPartPropertyDetails()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SPOSiteURL,
        [Parameter(Mandatory=$true)] [string] $pageRelativeURL,        
        [Parameter(Mandatory=$true)] [string] $UserName,
        [Parameter(Mandatory=$true)] [string] $Password
    )

    Try 
    {      

        $securePassword= $Password | ConvertTo-SecureString -AsPlainText -Force  
        #Setup the Context
        $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SPOSiteURL)
        $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $securePassword)

        $fileByServerRelativeUrl=$pageRelativeURL

        #Getting the page details
        $file = $ctx.Web.GetFileByServerRelativeUrl($fileByServerRelativeUrl)  
        $ctx.Load($file)  
        $ctx.ExecuteQuery()


       #Extract all the webparts from the given page.  
        Write-Host "Extracting all webparts from the given page."  
        $wpManager = $file.GetLimitedWebPartManager([Microsoft.SharePoint.Client.WebParts.PersonalizationScope]::Shared)  
        $webpartColls = $wpManager.Webparts  
        $ctx.Load($webpartColls)  
        $ctx.ExecuteQuery()  

        if($webpartColls.Count -gt 0)
        {  
        Write-Host "Looping through all webparts in the page."

        $webPartCount=1   
        foreach($oneWebpart in $webpartColls)
        {  
            $ctx.Load($oneWebpart.WebPart.Properties)  
            $ctx.ExecuteQuery()  
            $wpPropValues = $oneWebpart.WebPart.Properties.FieldValues  
            Write-Host "Webpart ID: " $oneWebpart.ID

            Write-Host $webPartCount

            Write-Host "##########################" -f Green

            foreach($oneProperty in $wpPropValues)
            {  



                Write-Host "Title: " $oneProperty.Title                  
                Write-Host "Description: " $oneProperty.Description  
                Write-Host "Chrome Type: " $oneProperty.ChromeType
                Write-Host "Back Ground Color: "$oneProperty.BackgroundColor
                Write-Host "Allow Edit: "$oneProperty.AllowEdit
                Write-Host "Allow Hide: "$oneProperty.AllowHide


                $listItemData = @{
                "Webpart Title" = $oneProperty.Title
                "Webpart Description" = $oneProperty.Description
                "Chrome Type" = $oneProperty.ChromeType 
                "Allow Edit" = $oneProperty.AllowEdit
                "Allow Hide" = $oneProperty.AllowHide
               }

                New-Object PSObject -Property $listItemData

            }


            Write-Host "##########################" -f Green  
            $webPartCount++  


        }  

    }  

     Write-host -f Green "The web part details successfully have been extracted."
    }
    Catch 
    {

            $ErrorMessage = $_.Exception.Message +"in reading web part properties!:" 
            Write-Host $ErrorMessage -BackgroundColor Red
            Write-Log $ErrorMessage 

    }

    return $listItemData
}

Call the above function like below:

#Parameters value
$siteURL="Your site URL"

$PageRelativeURL="/sites/yoursite/SitePages/your_webpart_page.aspx"     
$userName = "Your username"
$passWord = "Your Password"
#Parameters ends here.

#Calling the GetWebPartPropertyDetail function and passing the parameters.
GetWebPartPropertyDetails $siteURL $PageRelativeURL $userName $passWord | Export-Csv "C:\Temp\TestExport.csv"

Output:

Export webpart details to CSV file

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.