March 29, 2016

Create SharePoint Web Part Page using PowerShell

Hi,
 
I am working on PowerShell scripts to automate deployment for one of client,
 
Here is post :: Create SharePoint Web Part Page using PowerShell,


# Add SharePoint Snapin to PowerShell          
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {          
  Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}


#function to create web part page in Site Pages
function CreateBlankWebpartPage          
 
{
    Param($WebPartPageName)               
    try
    {    
        $pageLayout = 1  
        $spListCollection  = $OpenWeb.Lists 
        $spLibrary  = $spListCollection.TryGetList("Site Pages")       
        $xml = "" + $spLibrary.ID + "NewWebPageNewWebPartPage" + $pageLayout + "true" + $WebPartPageName + ""
        $OpenWeb.ProcessBatchData($xml)
        foreach($listItem in $spLibrary.Items)
        {
            if($($listitem.URL).Contains("$WebPartPageName" + ".aspx"))
            {
                $myListItem = $listItem
                break;
            }
        }
        $pageUrl =  $($myListItem.URL)
        Write-Host -f Green "New page is Created with Url - $pageUrl"
    }
    catch [System.Exception]
    {
        write-host -f red $_.Exception.ToString()
    }
}
 
 
$SiteURL = Read-Host -Prompt 'Enter Root Site URL and / @ end'
 
$OpenWeb = Get-SPWeb $SiteURL
 
CreateBlankWebpartPage -WebPartPageName "My Favorites"
CreateBlankWebpartPage -WebPartPageName "Pending Approval"
CreateBlankWebpartPage -WebPartPageName "My Tasks"
CreateBlankWebpartPage -WebPartPageName "My Reports"

No comments: