Tuesday, March 28, 2023

Powershell script to export all SEWP and CEWP content to CSv and text files

A working example to export all script editors and content editors content from an on-prem site collection. Handy for a SharePoint onprem to online migration project 


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

 

#Configuration parameters

$webApp = Get-SPWebApplication "http://yourwebappurl"


$csvFile = "<path to export folder>\pageswitheditors.csv"


$SiteURL = "http://yoursitecollurl"

$ReportOutput="<path to export folder>\pageswitheditors.csv"

 

$ResultCollection = @()

 

#Get All Subsites in a site collection and iterate through each

$Site = Get-SPSite $SiteURL

$counter=0;

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)

            {

             if ($webPart.GetType().Name -eq "ContentEditorWebPart" -or $webPart.GetType().Name -eq "ScriptEditorWebPart")

             {

             $counter+=1

             $content = $WebPart.Content.InnerText -replace "`n", " "

                $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() 

                 $Result | Add-Member -type NoteProperty -name "Web Part Content" -value "content$counter.txt" 

 

                $ResultCollection += $Result

               

                 $obj = New-Object -TypeName PSObject -Property @{

    Content = $content

}

$obj |Export-csv   "<path to export folder>\content$counter.txt" -notypeinformation -Encoding UTF8


         }}}

}

#Export results to CSV

$ResultCollection | Export-csv $ReportOutput -notypeinformation  -Encoding UTF8


No comments:

Post a Comment