Tuesday, 10 December 2013

Remove Custom Actions


<#
.Synopsis
   Deletes Custom Site Actions
.DESCRIPTION
       iterates over selected site or all sites, lists all custom site actions; allows user to select custom actions for deletion
.EXAMPLE
   Remove-spmSiteAction
.EXAMPLE
   Remove-spmSiteAction -$siteAppUrl http://wingtip.ca
#>
function Remove-spmSiteAction
{
    [CmdletBinding()]
    [OutputType([int])]
    Param
    (
        # Param1 help description
        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$True,
                   Position=0)]
        $siteAppUrl
    )

    Begin
    {
              #if site app url is not proviede as a parameter, prompt the user to pick one from the current farm
              if($siteAppUrl -eq $null -or $siteAppUrl -eq "")
              {
                     $myFarmServices = (Get-SPFarm).services | Where {$_.typename -like "*Web Application"}
                     $selection = -1
                     [string[]]$Apps = @()
                    
                     do{
                           $count = 0
                           foreach($webApp in $myFarmServices.WebApplications){
                                  Write-output ("{0}) {1}" -f $count++, $webApp.url)
                                  $Apps += $webApp.url
                           }
                           Write-output ("{0}) All" -f $count)
                           if(($selection = Read-Host "Please select the Site app you'd like to deploy to, -1 to quit") -eq -1){
                                  write-error ("Terminated cmdlet, user request") -erroraction stop
                           }
                     }while($selection -lt 0 -or $selection -gt $count)
                    
                     if($selection -eq $count){
                           $siteAppUrl = $Apps
                     }
                     else{
                           $siteAppUrl = $Apps[$selection]
                     }
              }
             
             
    }
    Process
    {
              $siteAppUrl | ForEach{
                     $site = Get-SPSite $_
                     $SiteActionsToRemove = @()
                    
                     if($SiteActionsToDelete.count -eq $null)
                     {
                           $selection
                           do
                           {
                                  Write-host "Please select the Custom action you wish to remove, 0 to remove selected entries, -1 to cancel"
                                  $count = 0
                                  $CustomSiteActionList = @()
                                  foreach($customAction in $site.UserCustomActions)
                                  {
                                         if(-not ($SiteActionsToRemove -contains $customAction.Id))
                                         {
                                                Write-Host (++$count) $customAction.Title, $customAction.Id
                                                $CustomSiteActionList += $customAction.Id
                                         }
                                  }
                                 
                                  $selection = Read-Host
                                 
                                  if(($selection -eq -1))
                                  {
                                         write-error ("Terminated cmdlet, user request") -erroraction stop
                                  }
                                  elseif($selection -gt $count)
                                  {
                                         Write-host "`nselection too high, please pick again "
                                  }
                                  elseif($selection -lt 0)
                                  {
                                         Write-host "`nselection too low, please pick again"
                                  }
                                  elseif($selection -gt 0)
                                  {
                                         $SiteActionsToRemove += $CustomSiteActionList[$selection-1]
                                  }
                           }while($selection -ne 0)
                          
                     }
                     Write-Host "`n"
                     if($SiteActionsToRemove -eq $null)
                     {
                           Write-Host No Site actions selected for deletion
                     }
                     else
                      {
                           foreach($customActionGuid in $SiteActionsToRemove)
                           {
                                  $customAction = $site.UserCustomActions[$customActionGuid]
                                  Write-host Deleteing Custom Action: $customAction.title With GUID: $customAction.id
                                  $customAction.Delete()
                           }
                     }
                     $site.dispose()
              }
    }
    End
    {
                    
    }

}