question

BhaskarPadmanabhan-4392 avatar image
0 Votes"
BhaskarPadmanabhan-4392 asked ElsieLu-MSFT commented

Fetching webpart contents using PNP powershell

I'm trying to migrate customized webpart data from onpremise to online in Content Editor webpart.

I'm copying the contents of the webpart from onpremise and creating a new content editor in the Sharepoint online and updating the contents of onpremise webpart.

Below code successfully fetched the text data but not pictures or hyperlinks.

function GetListOfWebpartDetails($SourceUrl, $TargetUrl) {

 Write-Host “Source url: $($SourceUrl)...." -ForegroundColor Green
 Connect-PnPOnline $SourceUrl -UseWebLogin
 $context = Get-PnPContext
 $site = $context.Web
 $context.Load($site)
 $context.ExecuteQuery()
    
 $items = Get-PnPListItem -List sitepages -PageSize 500
 if ($items) {
     foreach ($pages in $items) {  
            
         $pageName = $pages.FieldValues["FileLeafRef"]
         $PageUrl = "$($Site.SiteUrl)/SitePages/$pageName"
                   
         $lineArr = $PageUrl.Split(".")
         if ($lineArr[1]) {
             if ($lineArr[1].ToUpper().Contains('aspx'.ToUpper())) {

                 Connect-PnPOnline $SourceUrl -UseWebLogin
                 $WebPartManager = Get-PnPWebPart -ServerRelativePageUrl $PageUrl
                 #Get All Web Parts data
                 foreach ($WebPart in $WebPartManager) {
        
                     if ($WebPart.WebPart.Title.ToUpper().Contains('Test'.ToUpper())) {
                         write-Host $($WebPart.WebPart.Title)
                         $webpartProps = $webpart.WebPart.Properties.FieldValues

                         foreach ($webpartProp in $webpartProps) {                        
                             Write-Host "Content : " $webpartProp["Content"]    
                         }
                         Write-Host "-------------------------------------------"  
                         $targetPageURL = $target_site.ServerRelativeUrl + $PageUrl
                         $targetcontent = $webpartProp["Content"] 
 
                         $zoneid = Get-PnPProperty -ClientObject $webPart -Property ZoneId


                         #Adding Content Editor Webpart XML in $WebXml
                         $WebXml = "<WebPart xmlns='http://schemas.microsoft.com/WebPart/v2' xmlns:iwp='http://schemas.microsoft.com/WebPart/v2/ContentEditor'>
 <Title>$($WebPart.WebPart.Title)</Title>
 <FrameType>None</FrameType>
 <Description>$($webpartProp["Description"])</Description>
 <IsIncluded>true</IsIncluded>
 <ZoneID>$zoneid</ZoneID>
 <PartOrder>0</PartOrder>
 <FrameState>Normal</FrameState>
 <Height />
 <Width />
 <AllowRemove>true</AllowRemove>
 <AllowZoneChange>true</AllowZoneChange>
 <AllowMinimize>true</AllowMinimize>
 <AllowConnect>true</AllowConnect>
 <AllowEdit>true</AllowEdit>
 <AllowHide>true</AllowHide>
 <IsVisible>true</IsVisible>
 <DetailLink />
 <HelpLink />
 <HelpMode>Modeless</HelpMode>
 <Dir>Default</Dir>
 <PartImageSmall />
 <MissingAssembly>Cannot import this Web Part.</MissingAssembly>
 <PartImageLarge>/_layouts/15/images/mscontl.gif</PartImageLarge>
 <IsIncludedFilter />
 <Assembly>Microsoft.SharePoint, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
 <TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>
 <Content xmlns='http://schemas.microsoft.com/WebPart/v2/ContentEditor'>$targetcontent</Content>
 <PartStorage xmlns='http://schemas.microsoft.com/WebPart/v2/ContentEditor' />

</WebPart>"

                         Write-Host "Target url: $($TargetUrl)...." -ForegroundColor Green
                         Connect-PnPOnline $TargetUrl -UseWebLogin

                         try {
                                 
                             Add-PnPWebPartToWikiPage -ServerRelativePageUrl $targetPageURL -XML $WebXml -Row 1 -Column 1
                             Write-Host "Migrated Webparts Succesfully"
                         }
                         catch {
                             Write-Host "Page not found/Errors found:`n$_" -ForegroundColor Red
                    
                         }
                        
                            

                     }                        
                 }
             }
         }               
     }
 }

}

Appreciate if anyone can provide any lead.

office-sharepoint-onlinesharepoint-devoffice-sharepoint-server-development
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ElsieLu-MSFT avatar image
0 Votes"
ElsieLu-MSFT answered ElsieLu-MSFT commented

Hi @BhaskarPadmanabhan-4392 ,

If it is a migration, I would suggest you directly consider SPMT instead of powershell. SPMT, as a migration tool recommended by Microsoft, is very convenient to migrate SharePoint web parts from on-prem to SPO:
Download and install the SharePoint Migration Tool (SPMT)

Reference:
SPMT-supported features


If the 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.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @BhaskarPadmanabhan-4392 , is there any update? Have you tried SPMT?

0 Votes 0 ·
pkbullock avatar image
0 Votes"
pkbullock answered

If you are looking to move to the modern experience, check out the modernization tooling: modernize-classic-sites


https://docs.microsoft.com/en-us/sharepoint/dev/transform/modernize-userinterface-site-pages


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.