<pre class=
"brush:ps"
>
[Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges(
{
# Add the
PowerShell Snapin
$snapin = Get-PSSnapin | Where-Object {$_.Name
-eq 'Microsoft.SharePoint.Powershell'}
if ($snapin -eq $null)
{
Add-PSSnapin
"Microsoft.SharePoint.Powershell"
}
function
CreateSharePointPage($SiteUrl,$PageName,$PageTitle)
{
write-host "Get the PageLayout"
-foregroundcolor Green
$PageLayoutRelUrl =
"/_catalogs/masterpage/BlankWebPartPage.aspx"
write-host "Initialize the Site
Object" -foregroundcolor Green
# Initialize the Site Object
write-host $SiteUrl -foregroundcolor Green
$Site = Get-SPWeb -Identity $SiteUrl
write-host "Get the Publishing Site based
on the SPSite" -foregroundcolor Green
# Get the Publishing Site based on the SPSite
write-host "Get the SPWeb Object"
-foregroundcolor Green
# Get the SPWeb Object
$Web = Get-SPWeb($SiteUrl)
write-host "Initialize the Publishing Web
based on the SPWeb" -foregroundcolor Green
# Initialize the PublishingWeb based on the
SPWeb
$PubWeb =
[Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($Web)
write-host "Get the PageLayouts Installed
on the Publishing Site" -foregroundcolor Green
# Get the PageLayouts Installed on the
Publishing Site
$pl = $pubWeb.GetAvailablePageLayouts() |
Where { $_.Name -eq "BAYADA_BlankWebPartPage.aspx" }
#$Layouts = $PubSite.GetPageLayouts($True)
# Get our PageLayout
#$PageLayout = $Layouts[$PageLayoutRelUrl]
write-host "Create a new publishing
page." -foregroundcolor Green
$Page= $null;
# Create a new publishing page.
try
{
write-host "Getting Existing file "
$ExistingPageUrl=$SiteUrl+"Pages/"+$PageName;
$PublishingPage =
$Web.GetFile($ExistingPageUrl);
write-host $ExistingPageUrl
write-host $PublishingPage
write-host "Doing Check out"
$PublishingPage.CheckOut()
write-host "Deleting file"
$PublishingPage.Delete()
}
Catch{}
try
{
$Page = $PubWeb.AddPublishingPage($PageName,
$pl)
}
Catch
{
write-host $_.Exception.Message
-ForegroundColor Red
#$ErrorMessage = $_.Exception.Message
# Get only our Page
}
write-host "Assign the Title for the
Page." -foregroundcolor Green
# Assign the Title for the Page
$Page.Title = $PageTitle
write-host "Update the page"
-foregroundcolor Green
# Update the Page
$Page.Update();
write-host "Check in the Page with
Comments" -foregroundcolor Green
# Check in the Page with Comments
$Page.CheckIn("Automatic Check in")
write-host "Publish the Page With
Comments" -foregroundcolor Green
# Publish the Page With Comments
$Page.ListItem.File.Publish("Automatic
Publish Comment")
}
function AddPageViewerWebPart($SiteURL,
$pageUrl, $Zone, $title,$contentLink)
{
# Add the PowerShell Snapin
$snapin = Get-PSSnapin | Where-Object {$_.Name
-eq 'Microsoft.SharePoint.Powershell'}
if ($snapin -eq $null)
{
Add-PSSnapin
"Microsoft.SharePoint.Powershell"
}
Start-SPAssignment -Global
# $spWeb = Get-SPWeb -Identity "http://localhost:32712/"
write-host "Creating SPWeb object"
-foregroundcolor Green
$web = Get-SPWeb -Identity $SiteURL
write-host "Getting current
webpage object" -foregroundcolor
Green
$page =
$web.GetFile($pageUrl)
write-host "Performing checkout for
current webpage" -foregroundcolor Green
$page.CheckOut()
write-host "Getting webpartmanager" -foregroundcolor Green
$webpartmanager=$web.GetLimitedWebPartManager($pageUrl,
[System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
write-host "Creating Page Viewer WebPart" -foregroundcolor
Green
$webpart = new-object
Microsoft.SharePoint.WebPartPages.PageViewerWebPart;
$webpart.Title = $title
$webpart.ChromeType=[System.Web.UI.WebControls.WebParts.PartChromeType]::TitleOnly;
$webpart.Height = "9in";
write-host "Setting ContentLink" -foregroundcolor Green
$webpart.ContentLink = $contentLink;
write-host "Adding Page Viewer webpart in Center"
-foregroundcolor Green
$webpartmanager.AddWebPart($webpart,$Zone,2);
write-host "Performing check in for current webpage"
-foregroundcolor Green
$page.CheckIn("Automatic CheckIn")
write-host "Publishing current
page" -foregroundcolor Green
write-host $page
$page.Publish("Automatic Publish
Comment");
write-host "Done" -foregroundcolor Green
$web.Close()
#$spWeb.Close()
}
#Example to call these methods to create sharepoint page
#Site url where we need to create/Edit page
$SiteUrl= "http://sphome.devtest.bayada.com/payroll/" ;
#Page Name
$PageName="UpdateOriginalHireDate.aspx";
#Page Title
$PageTitle="Update Original Hire Date";
#Page full Url to Add the webpart
$PageUrl="http://localhost:3434/teamsite/test.aspx";
#Specify the Zone where you want to place the WebPart Ex: Header,Center,Footer
$Zone="Header";
#Content Title
$ContentTitle=" ";
#URL link to set in the PageViewerWebPart
$ContentUrl="/_apps/ors/GetUsers";
#Specify the Header where the left Navigation node tobe added
$Header="Finance";
CreateSharePointPage $SiteUrl $PageName $PageTitle;
AddPageViewerWebPart $SiteUrl $PageUrl $Zone $ContentTitle $ContentUrl ;
</pre>
#Example to call these methods to create sharepoint page
#Site url where we need to create/Edit page
$SiteUrl= "http://sphome.devtest.bayada.com/payroll/" ;
#Page Name
$PageName="UpdateOriginalHireDate.aspx";
#Page Title
$PageTitle="Update Original Hire Date";
#Page full Url to Add the webpart
$PageUrl="http://localhost:3434/teamsite/test.aspx";
#Specify the Zone where you want to place the WebPart Ex: Header,Center,Footer
$Zone="Header";
#Content Title
$ContentTitle=" ";
#URL link to set in the PageViewerWebPart
$ContentUrl="/_apps/ors/GetUsers";
#Specify the Header where the left Navigation node tobe added
$Header="Finance";
CreateSharePointPage $SiteUrl $PageName $PageTitle;
AddPageViewerWebPart $SiteUrl $PageUrl $Zone $ContentTitle $ContentUrl ;
</pre>