Thursday, 12 December 2013

Create Publishing web, Pages, approve them

My Task is to copy a bunch of publishing pages into an "Archived Site", before you think the obvious that is just a dumb approach, this script is for a bull headed client. Rather then try and explain, it's just best to give them what they want even if it's not what they need. To get started I created a script that would populate a newsroom subsite with subsites for each month and add 5 checked in, approved pages to each of the subsites. Here is that Script:



$months = "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"

#delete the subsites
foreach($month in $months)
{
       $w = get-SPWeb http://ssrap1:8080/NewsRoom/$month
       Remove-SPWeb -Identity $w -Confirm
}

#create the subsite for the newsroom
foreach($month in $months)
{
       $SiteTemplate = get-SPWebTemplate | where {$_.Title -eq "Publishing Site with Workflow"}
      
       $web = New-SPWeb -Url http://ssrap1:8080/NewsRoom/$month  -Name $month -Template $SiteTemplate
       Write-Host "`nCreate SubSite: $web"
      
       $pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
      
       $i = 0
       #create 5 pages for subsite
       Write-Host Createing Pages for $month subsite
       while ($i -le 5)
       {
              $newPage = $pWeb.AddPublishingPage();
              $newPage.CheckIn("Checked in")
              $i++
       }

       $pages = $pWeb.GetPublishingPages()
      
       #approve all pages
       foreach($p in $pages)
       {
                $p.ListItem.File.Approve("Page approved automatically by PowerShell script")
       }            

       $web.dispose()
}

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
    {
                    
    }

}