3

I need to learn a PS script to create a WebPart Page on any library I want in my Sharepoint 2010 site. Im brand new to PowerShell and Sharepoint, so far all I know is the little tought on a few MVAs I've done, so please go easy on the explanations lol

Thanks in advance, I hope to return the favor someday!

1

If you want to create a webpart page, then understand below parameters which we will use in the function.

  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

Refer the full function below (Referred from: SharePoint 2010: Create a Blank WebPart Page using PowerShell )

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
}

Usage:

$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
  • There is XML missing, just as in the source article, at this line: $cmd ='' + $pagesLibrary.ID + 'NewWebPageNewWebPartPage' + $pageLayout + 'true'+ $WebPartPageName +''; – JanErikGunnar Oct 27 '17 at 10:14
2

Click the below msdn link ,This example script can be used to create blank webpart page in a Non-Publishing SharePoint web

http://social.technet.microsoft.com/wiki/contents/articles/19610.sharepoint-2010-create-a-blank-webpart-page-using-powershell.aspx

1

There is this beautiful blogpost which explains the PS script to create WebPart Page in any library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.