Create Blank WebPart Page Using PowerShell

Create Blank WebPart Page Using PowerShell

Use CreateBlankWebpartPage function to create blank webpart page in a Non-Publishing SharePoint web.

Parameters : 
  1. $webUrl - Mandatory - SharePoint Web Url - e.g. http://server:port/ 
  2. $WebPartPageName - Mandatory - New page name
  3. $WebPartPageLibraryName - Mandatory - Parent Library name for new page
  4. $pageLayout - Mandatory - Page Layout code - Int


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 ='<?xml version="1.0" encoding="UTF-8"?><Method ID="0,NewWebPage"><SetList Scope="Request">' + $pagesLibrary.ID + '</SetList><SetVar Name="Cmd">NewWebPage</SetVar><SetVar Name="ID">New</SetVar><SetVar Name="Type">WebPartPage</SetVar><SetVar Name="WebPartPageTemplate">' + $pageLayout + '</SetVar><SetVar Name="Overwrite">true</SetVar><SetVar Name="Title">'+ $WebPartPageName +'</SetVar></Method>';

        $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 


}

Function  Calling -

$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
 For more details on NewWebPage method , please check this -
Leave a Comment
  • Please add 2 and 1 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Richard Mueller edited Revision 1. Comment: Removed extra space in tag "SharePoint  2010", removed dash in tag "Power-Shell"

  • Maheshkumar S Tiwari edited Original. Comment: Added tags

Page 1 of 1 (2 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Maheshkumar S Tiwari edited Original. Comment: Added tags

  • Richard Mueller edited Revision 1. Comment: Removed extra space in tag "SharePoint  2010", removed dash in tag "Power-Shell"

Page 1 of 1 (2 items)