Share via


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.

  1. Full Page, Vertical
  2. Header, Footer, 3 Columns
  3. Header, Left Column, Body
  4. Header, Right Column, Body
  5. Header, Footer, 2 Columns, 4 Rows
  6. Header, Footer, 4 Columns, Top Row
  7. Left Column, Header, Footer, Top Row, 3 Columns
  8. Right Column, Header, Footer, Top Row, 3 Columns

See Also