Question

I customized the OOTB web parts in SharePoint 2013 server, I need to move my web parts to another server without taking site backup, I tried Export options it's working fine ,But It's very difficult to add manually, So is it possible to deploy the exported web part programmatically by using scripts. Please suggest some idea?

Was it helpful?

Solution

I think the difficulty at manual method that you try to upload it one by one , so If you don't have many web parts and pages , you can upload all the exported web part at once at Web Designer Gallary then add it easily at your page as the following

  • Open Root Site > Site Settings > Below Web Designer Gallary > click on Web Parts.
  • From the above ribbon , you can upload all the exported web parts then you can set a meaning name at a group field to show all the exported web parts at the customized category.

  • Go to your page now , add web part , you should now find the custom category with all exported web parts that should be shown for all pages within the site.

If you need to do this via script at SharePoint Server 2013 on-prem , Try to run this script

# Add PowerShell Snapin
 $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
 if ($snapin -eq $null) 
 { 
     Add-PSSnapin "Microsoft.SharePoint.Powershell"
 }

 # Get the Site URL
 $SiteUrl = "https://MySiteCollectionURL/"

 # Get the Web URL
 $WebUrl = "https://MyWebSiteURL"

 # Get the Page on which WE are going to Add the WebPart
 $PageName = "Test.aspx"

 # The location of the WEbPart Definition File
 $localWebpartPath = "C:\Windows\System32\MyWebPart.webpart"

 # Error Message which is required as a Reference Parameter while Importing the WEbPart
 $errorMsg = "Test Error Message"

 # Initializing the SPSite Object
 $Site = Get-SPSite($SiteUrl)

 # Get an instance for Publishing Site based on SPSite
 $PubSite = New-Object Microsoft.SharePoint.Publishing.PublishingSite($Site)

 # Get the SPWEb Object
 $Web = Get-SPWeb $WebUrl

 # Get the Publishing Web Based on the SPWeb Object
 $PubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($Web)

 # The below commented line is to get all the Pages
 #$PubWeb.GetPublishingPages($PageName);

 # Get only our Page
 $PublishingPage = $PubWeb.GetPublishingPage("https://MyWebURL/Pages/Test.aspx");

 # Make the Web as AllowUnSafeUpdates as true
 $Web.AllowUnsafeUpdates = $true

 # Checkout the Publishing Page
 $PublishingPage.CheckOut();

 # Get the LimitedWEbPartManager
 $limitedWebPartManager = $PublishingPage.ListItem.File.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared);

 # Initialize the XmlReaderSettings Object which is required for the XmlReader Object
 $xmlReaderSettings = New-Object System.Xml.XmlReaderSettings

 # Create the XmlReader Object by using the WebPart Definition file and the ReaderSettings Object
 $xmlReader = [System.Xml.XmlReader]::Create($localWebpartPath,$xmlReaderSettings);

 #Add Web Part to catalogs folder and Get the WebPart Definition Object based on the webpart definition xml file     
 $oWebPartDefinition = $limitedWebPartManager.ImportWebPart($xmlReader,[ref]$errorMsg); 

 # Add the WebPart to the WebPartManager by specifing the Zone and the Index.
 $limitedWebPartManager.AddWebPart($oWebPartDefinition,"RightZone",1);

 # Checkin the Publishing Page
 $PublishingPage.CheckIn("Test Checkin by Sathish");

 # Publish the Page
 $PublishingPage.ListItem.File.Publish("Test Publish By Sathish");

 # Revert the AllowUnsafeUpdates to False once we are done.
 $Web.AllowUnsafeUpdates = $false

 # I was trying to Approve the Page.  But, if the Approve is enabled on the Pages Library level, 
 # then only we can do that.  Otherwise we cannot.  But the Syntax is correct as below.

 # $PageListItem.File.Approve("Page approved automatically by PowerShell script")  

Ref for this snippet is How to Add WebPart to the Publishing Page using PowerShell in SharePoint 2013

OTHER TIPS

The OfficeDev PnP PowerShell Commands has a Cmdlet that could allow you to do this from a PowerShell script using an exported webpart:

Add-SPOWebPartToWebPartPage

Add-SPOWebPartToWikiPage

You can use this like

Connect-SPOnline https://yourspsite.com
Add-SPOWebPartToWebPartPage -ServerRelativePageUrl "default.aspx" -Path "MyExported.webpart" -ZoneId "Left" -ZoneIndex 0
Disconnect-SPOnline

You can then adjust the above to loop through a list of sites.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top