Search This Blog

Thursday 14 June 2012

Create webpart page in SharePoint 2010 through PowerShell(Publishing Page)



Recently I had a situation to create multiple webpart page. It’s not suitable to do this in OOB(doing as default).  So I have gone with PowerShell script.

After a minimal number of tries, I achieved this, I posted the script below.


if((Get-PSSnapin "Microsoft.SharePoint.PowerShell" ) -eq $null)
{
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Publishing")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Publishing.PublishingWeb")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Publishing")
[System.Reflection.Assembly]::LoadWithPartialName("System")


$site = new-object Microsoft.SharePoint.SPSite("http://chesgh201tw1v:2020")
$web = $site.OpenWeb();

$pubWeb =[Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
# Create blank web part page
$pl = $pubWeb.GetAvailablePageLayouts() | Where { $_.Name -eq "BlankWebPartPage.aspx" } #you may change "BlankWebPartPage.aspx" to your custom page layout file name
$newPage = $pubWeb.AddPublishingPage("myfile.aspx", $pl) #filename need end with .aspx extension
$newPage.Update()
# Check-in and publish page
$newPage.CheckIn("")
$newPage.ListItem.File.Publish("")
$web.Dispose()

you can download the script here

Note: SharePoint Publishing feature should be in active state to run the above script successfully. Here i posted single peace of my code(Create single page).

No comments:

Post a Comment