SharePoint 2010: Create a Blank WebPart Page using PowerShell
Introduction
This example script can be used to create a blank webpart page in a Non-Publishing SharePoint web.
Script Parameters
- $webUrl - Mandatory - SharePoint Web Url - e.g. http://server:port/
- $WebPartPageName - Mandatory - New page name
- $WebPartPageLibraryName - Mandatory - Parent Library name for new page
- $pageLayout - Mandatory - Page Layout code - Int
Script Example
Below you see the script sample.
function CreateBlankWebpartPage { [CmdletBinding()] Param( [Parameter(Mandatory=$true,ValueFromPipeline=$true)] [string]$webUrl, [Parameter(Mandatory=$true)] [string]$WebPartPageName, [Parameter(Mandatory=$true)] [string]$WebPartPageLibraryName, [Parameter(Mandatory=$true)] [int]$pageLayout ) Start-SPAssignment -Global $spWeb = Get-SPWeb -Identity $webUrl $pagesLibrary = $spWeb.Lists | Where { $_.Title -eq $WebPartPageLibraryName } if ($pagesLibrary -eq $null) { Write-Host -f red "WebPart Page Library - $WebPartPageLibraryName Not exists." return } Write-Host -f yellow "Creating New Webpart page $WebPartPageName in library $WebPartPageLibraryName" [Microsoft.SharePoint.SPFile]$spFile = $spWeb.GetFile("/" + $pagesLibrary.RootFolder.Url + "/" + $WebPartPageName + ".aspx") $flagConfirm = 'y' if($spFile.Exists -eq $true) { $flagConfirm = Read-Host "Page $WebPartPageName already exists in library $WebPartPageLibraryName, do you want to recreate(y/n)?" } if ($flagConfirm -eq 'y' -or $flagConfirm -eq 'Y') { Write-Host -NoNewLine -f yellow "Creating New Webpart page $WebPartPageName " $cmd ='' + $pagesLibrary.ID + 'NewWebPageNewWebPartPage' + $pageLayout + 'true'+ $WebPartPageName +''; $spWeb.ProcessBatchData($cmd) write-host -f Green "...Success!" #Add weppart in Webpart page foreach($listItem in $pagesLibrary.Items) { if($($listitem.URL).Contains("$WebPartPageName" + ".aspx")) { $myListItem = $listItem break; } } $pageUrl = $($myListItem.URL) Write-Host -f yellow "New page Url - $pageUrl" } Stop-SPAssignment -Global }
Calling the Function
$webUrl = "http://sever:port" $WebPartPageLibraryName = "DocLib1" $WebPartPageName = "MyWPPage" $pageLayout = 1 CreateBlankWebpartPage $webUrl $WebPartPageName $WebPartPageLibraryName $pageLayout
These are page layouts available for New page.
- Full Page, Vertical
- Header, Footer, 3 Columns
- Header, Left Column, Body
- Header, Right Column, Body
- Header, Footer, 2 Columns, 4 Rows
- Header, Footer, 4 Columns, Top Row
- Left Column, Header, Footer, Top Row, 3 Columns
- Right Column, Header, Footer, Top Row, 3 Columns