Use CreateBlankWebpartPage function to create blank webpart page in a Non-Publishing SharePoint web.
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
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
}
{
[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
$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
For more details on NewWebPage method , please check this -
No comments:
Post a Comment