I need to set the web part I added to a provider hosted app as a default page in the site via a module. Can someone tell me please how to modify that module in order to load the web part as a default page in my SP site?


If I understood your question, you want to add a web part to a page and set it as default.

So you can add the web part in this way:

# 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:\WebParts\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

 # 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

 # 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.

 # Checkin the Publishing Page

 # Publish the Page

 # 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") 

- See more at: http://www.sharepointpals.com/post/How-to-Add-WebPart-to-the-Publishing-Page-using-PowerShell-in-SharePoint-2013#sthash.xcdNmxZv.dpuf

And set the page as default:

$site = Get-SPSite http://yourserver/sites/yoursite
$web = $site.RootWeb
  (or $web = $site.OpenWeb("yoursubsite")
$folder = $web.RootFolder
$folder.WelcomePage = "SitePages/home.aspx"
  (or  $folder.WelcomePage = "default.aspx")
  (or  $folder.WelcomePage = "Shared%20Documents/mycustomwebpartpage.aspx")

How to change the default home page
How to Add WebPart to the Publishing Page using PowerShell in SharePoint 2013

| improve this answer | |
  • I want to set my web part as a default page in the site using feature, how to do that please? – N.Sahraoui Apr 20 '15 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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