Utilisation de Powershell pour ajouter/supprimer/mettre à jour des webparts sur SharePoint 

Tags: SharePoint, Powershell

 

Dans le cadre de mon travail je suis amené à réaliser différents scripts Powershell pour SharePoint 2007 et 2010 afin d’automatiser certaines opérations.

 

Ci-dessous un script powershell qui permet

  • D’ajouter une webpart
  • De mettre à jour (Supprimer puis ajouter) une webpart
  • Supprimer une webpart

 

Ce script est sous la forme d’une fonction powershell qui utilise :

 

Prérequis:

  • SharePoint 2007 ou 2010
  • Page ayant une zone de webpart
  • Pour insérer une webpart : sous la forme .dwp ou .webpart

 

Exemples :

AddUpdateDeleteWebpart -Url "http://localhost:4242/Pages/default.aspx" -Add -WebPartFile 'D:\MaWebpart.webpart' -ZoneID "BottomLeftZone" -ZoneIndex 1
AddUpdateDeleteWebpart -Url "http://localhost:4242/Pages/default.aspx" -Update -WebPartFile 'D:\MaWebpart2.webpart'-IdentityField "Title" -IdentityValue "Ma première webpart"
AddUpdateDeleteWebpart -Url "http://localhost:4242/Pages/default.aspx" -Delete -IdentityField "Title" -IdentityValue "Ma seconde webpart"
AddUpdateDeleteWebpart -Url "http://localhost:4242/Pages/default.aspx" -UpdateOrAdd -WebPartFile "D:\MaWebPart3.webpart" -ZoneID "BottomRightZone" -ZoneIndex 1  -IdentityField "Title" -IdentityValue "Ma seconde webpart"
 

Le code :
 
function AddUpdateDeleteWebpart
{
 
<#
.SYNOPSIS
Add / Update / Delete Web part
 
#>
 
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true, ParameterSetName="Add")]
        [Parameter(Mandatory=$true, ParameterSetName="Update")]
        [Parameter(Mandatory=$true, ParameterSetName="Delete")]
        [Parameter(Mandatory=$true, ParameterSetName="UpdateOrAdd")]
        [string]$Url,
        
        [Parameter(Mandatory=$true, ParameterSetName="Add")]
        [Parameter(Mandatory=$true, ParameterSetName="Update")]
        [Parameter(Mandatory=$true, ParameterSetName="UpdateOrAdd")]
        [string]$WebPartFile,
        
        [Parameter(ParameterSetName="Add")]
        [switch]$Add,
        
        [Parameter(ParameterSetName="Update")]
        [switch]$Update,
        
        [Parameter(ParameterSetName="UpdateOrAdd")]
        [switch]$UpdateOrAdd,
        
        [Parameter(ParameterSetName="Delete")]
        [switch]$Delete,
        
        [Parameter(Mandatory=$true, ParameterSetName="Update")]
        [Parameter(Mandatory=$true, ParameterSetName="Delete")]
        [Parameter(Mandatory=$true, ParameterSetName="UpdateOrAdd")]
        [string]$IdentityValue,
        
        [Parameter(Mandatory=$true, ParameterSetName="Update")]
        [Parameter(Mandatory=$true, ParameterSetName="Delete")]
        [Parameter(Mandatory=$true, ParameterSetName="UpdateOrAdd")]
        [string]$IdentityField,
 
        [Parameter(Mandatory=$true, ParameterSetName="Add")]
        [Parameter(Mandatory=$true, ParameterSetName="UpdateOrAdd")]
        [string]$ZoneID,
        
        [Parameter(Mandatory=$true, ParameterSetName="Add")]
        [Parameter(Mandatory=$true, ParameterSetName="UpdateOrAdd")]
        [int]$ZoneIndex
    )
    BEGIN
    {
        echo "BEGIN"
    }
    PROCESS
    {    
        [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
        try
        {
            $site = New-Object Microsoft.SharePoint.SPSite($Url)
            $web = $site.OpenWeb()
            
            
            try
              {
                $f = $web.GetFile($url)
              }
              catch {  }
              if (-not $f -or -not $f.Exists)
              {
                  throw "Cannot Add/Update/delete webpart : page does not exist at Url $url"
            }
            
            if ($f.Level -eq [Microsoft.SharePoint.SPFileLevel]::Checkout)
              {
                Write-SPMessage "File already Checkout"
              }
            else
            {
                $f.CheckOut()
            }
            
            $wpm = $f.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
            
            if ($Add -or $UpdateOrAdd -or $Update)
            {
                try
                {
                    $fs = [IO.File]::OpenRead($WebPartFile)
                }
                catch{}
                if (-not $fs)
                {
                    throw "Cannot Add/Update webpart : Webpart file $WebPartFile does not exist. $Error"
                }
                
                try
                {
                    $xmlreader = [System.Xml.XmlReader]::Create($fs)
                }
                catch { }
                if (-not $xmlreader)
                {
                    throw "Cannot Add/Update webpart : Webpart file $WebPartFile is not a valid XML file. $Error"
                }
                $importerror = ""
                $webpart = $wpm.ImportWebPart($xmlreader, [ref]$importerror);
                if (-not $webpart -or $importerror)
                {
                    throw "Cannot Add/Update Webpart : Cannot import webpart $WebPartFile. $importerror"
                }
            }
            
            if ($Delete -or $Update -or $UpdateOrAdd)
            {
                $webparts = @()
                $wpm.WebParts | %{
                    if ($_.$IdentityField -eq $IdentityValue)
                    {
                        $webparts += $_
                    }
                }
                
                if ($Delete)
                {
                    if ($webparts)
                    {
                        $webparts |%{
                            echo "Deleting Webpart $($_.Title)"
                            $wpm.DeleteWebPart($_)
                        }
                    }
                }
                else
                {
                    if ($webparts)
                    {
                        $webparts |%{
                            echo "Deleting and recreating Webpart $($_.Title)"
                            $ZoneID = $wpm.GetZoneID($_)
                            $ZoneIndex = $_.ZoneIndex
                            $wpm.DeleteWebPart($_)
                            $wpm.AddWebPart($webpart, $ZoneID, $ZoneIndex)
                            $wpm.SaveChanges($webpart)
                        }
                    }
                }
            }
                    
            if ($Add -or ($UpdateOrAdd -and -not $webparts))
            {
                echo "Adding webpart $WebPartFile to $Url"
                 $wpm.AddWebPart($webpart, $ZoneID, $ZoneIndex)
                $wpm.SaveChanges($webpart)
                echo "Adding webpart $WebPartFile to $Url DONE"
            }
                
            $f.CheckIn("Update Webpart", [Microsoft.SharePoint.SPCheckinType]::MajorCheckIn)
            if ($f.Item.ModerationInformation -and $f.Item.ModerationInformation.Status -eq [Microsoft.SharePoint.SPModerationStatusType]::Pending)
            {
                $f.Approve("Update Webpart");
            }
            
        }
        finally
        {
            if ($web)
            {
                $web.Dispose()
            }
            if ($site)
            {
                $site.Dispose()
            }
            if ($fs)
            {
                $fs.Dispose()
            }
        }
    }
    END
    {
        echo "END"
    }
}
 
Posted by Sylvain on 28-Oct-11
0 Comments  |  Trackback Url  |  Link to this post | Bookmark this post with:        
 

Links to this post

Comments

Nom *:
URL:
Email:
Commentaires:
CAPTCHA Image Validation