Aggiungere una Web Part in PowerShell
Con questo script in PowerShell 1.0 è possibile aggiungere una ListViewWebPart ad una pagina di SharePoint 2007 (WSS3 - MOSS).
Utile per aggiungere una web part presente in un altro sito della site collection.
per eseguirlo
una volta inserita nella pagina è possibile gestirla da browser come una qualsiasi web part.
Vedi anche Aggiungere una Web Part tramite codice lo stesso codice realizzato in C#.
Utile per aggiungere una web part presente in un altro sito della site collection.
PowerShell: AddListViewWebPart.ps1
#add ListViewWebPart to a page
#set-executionpolicy remotesigned
param(
[string]$UrlPage = $(throw "UrlPage: Page url is required."),
[string]$UrlList= $(throw "UrlList: list/view url is required."),
[string]$ZoneID="Main",
[int]$ZoneIndex = 1
)
[void] [Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[void] [Reflection.Assembly]::LoadWithPartialName("System.Web")
$ErrorActionPreference = "stop"
write-host "Page: $urlPage" -foregroundcolor green
write-host "List: $urlList" -foregroundcolor green
write-host "zoneID: $zoneID" -foregroundcolor green
write-host "ZoneIndex: $zoneIndex" -foregroundcolor green
#open destination page
write-host "Opening $urlPage ..." -foregroundcolor green
$sitePage = new-object microsoft.sharepoint.SPSite $urlPage
$webPage = $sitePage.OpenWeb()
$filePage = $webPage.GetFile($urlPage)
#open list view
write-host "Opening $urlList ..." -foregroundcolor green
$siteList = new-object microsoft.sharepoint.SPSite $urlList
$webList = $siteList.OpenWeb()
$view = $webList.GetViewFromUrl($urlList)
#create a web part
$lvw = new-object microsoft.sharepoint.WebPartPages.ListViewWebPart
$lvw.WebId = $webList.ID
$lvw.ListName = $view.ParentList.ID.ToString("B").ToUpper()
#$lvw.ViewGuid = $view.ID.ToString("B");
$lvw.TitleUrl = $view.Url
$lvw.Title = $view.Title + " - " + (Get-Date)
$lvw.AllowClose = $false
$lvw.AllowConnect = $false
$lvw.AllowHide = $false
$lvw.AllowMinimize = $false
$lvw.AllowRemove = $false
#add standard ListViewWebPart to a page
$checkin = $false
if( $filePage.InDocumentLibrary
-and $filePage.CheckOutStatus -eq "None") {
write-host "CheckOut" -foregroundcolor green
$filePage.CheckOut()
$checkin = $true
}
write-host "Adding ListViewWebPart ..." -foregroundcolor green
$wpm = $webPage.GetLimitedWebPartManager($urlPage
, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared);
$wpm.AddWebPart($lvw, $zoneID, $zoneIndex);
$lvw.Dispose()
$wpm.Dispose()
if($checkin -eq $true
-and $filePage.InDocumentLibrary
-and $filePage.CheckOutStatus -ne "None") {
$filePage.CheckIn("Added by PowerShell")
write-host "CheckIn" -foregroundcolor green
}
$webList.Dispose()
$siteList.Dispose()
$webPage.Dispose()
$sitePage.Dispose()
write-host "Finished" -foregroundcolor green
DOS / Batch file
.\AddListViewWebPart.ps1 "http://sharepoint.sgart.it/Docs/Pages/default.aspx" http://sharepoint.sgart.it/Lists/Elenco1/View1.aspx
Vedi anche Aggiungere una Web Part tramite codice lo stesso codice realizzato in C#.