Wednesday, 18 December 2013

Top Document Authors

I work in municipal government in Canada, we are having legislation come through that requires new documents (pdf's, docx, etc) to be accessible. To begin our training of content creators we required a way to identify our number one document contributors, this is the script to accomplish this:

<#
.Synopsis
   List Users by number of Documnets
.DESCRIPTION
   This script lists all docuemnt authors by the number of docs they've created
.EXAMPLE
  Get-SPMAuthors -SiteUrl http://ssrap1 -OutputPath c:\output.csv
#>
function Get-SPMAuthors
{
    [CmdletBinding()]
    [OutputType([int])]
    Param
    (
        # the site app url ie http://wingtipserver
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$False,
                   Position=0)]
              [string]
        $SiteUrl,
              # pass in the output path for the csv
              [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$False,
                   Position=1)]
              [string]
              $OutputPath         
    )

    Begin
    {
              $SiteApp = Get-SPSite $SiteUrl
              $table = New-Object system.Data.DataTable "Documents"
              $col1 = New-Object system.Data.DataColumn CreatedBy,([string])
              $table.columns.add($col1)
    }
    Process
    {
              $SiteApp.AllWebs | % {
                     $docLib = $_.Lists["Documents"]
                     $docLib.Items | % {
                           $row = $table.NewRow()
                           $row.CreatedBy = $_["Created By"]
             
                           $table.Rows.Add($row)
                     }     
              }
             
              $groupedTable = $table | Group-Object CreatedBy | sort Count –Descending
              $groupedTable | select Count, Name | export-csv $OutputPath -noType
    }
    End
    {
              $col1.Dispose()
              $table.Dispose()
              $SiteApp.Dispose()
    }

}

Friday, 13 December 2013

Move Publishing Pages

In my last post I laid out the requirements of my script and a script to set up a source site to copy from, now here is the script that does the movement of all documents, images, and pages from a web that has a bunch of subwebs where the news articles are stored. Again I know this is not the SharePoint way, but it's the way the thing is set up, my job was not to fix this.

<#
.Synopsis
   Script to archive newsroom assets
.DESCRIPTION
   Script to migrate Newsroom Assets to archive Folder
.EXAMPLE
   Copy-spmNewsroomAssets -SourceWebUrl http://spm/newsroom -DestinationWebUrl http://spm/archive/2013
.EXAMPLE
   Copy-spmNewsroomAssets -SourceWebUrl http://spm/newsroom -DestinationWebUrl http://spm/archive/2013 | Out-File c:\Failed.txt
#>

function Copy-SpmNewsroomAssets
{
    [CmdletBinding()]
    Param
    (
        # Specify the newsroom web, it should poses subwebs for each month ie http://www.spm/newsroom
        [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$false, Position=0)]
              [string]
        $SourceWebUrl,

        # Specify the archive web ie http://www.spm/Archive/2013
        [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$false, Position=1)]
              [string]
        $DestinationWebUrl
    )

    Begin
    {
              $SourceWeb = Get-SPWeb $SourceWebUrl
              $DestinationWeb = Get-SPWeb $DestinationWebUrl
              $folders = "Documents", "PublishingImages", "Pages"
    }
    Process
    {
              $folders | % {
                     Move-Files -SourceWeb $SourceWeb -DestinationWeb $DestinationWeb -folder $_
              }
    }
    End
    {
              $SourceWeb.Dispose()
              $DestinationWeb.Dispose()
    }
}

function Move-Files($SourceWeb, $DestinationWeb, $folder)
{
      
       $DestinationFolder = $DestinationWeb.GetFolder($folder)
      
       $SourceWeb.webs | % {
              $SourceLibrary = $_.GetFolder($folder)
                      
              foreach($file in $SourceLibrary.Files)
              {
                     if(!(Check-FileExists -filename $file.Name -folder $DestinationFolder))
                     {
                            $destinationFile = $DestinationFolder.Files.Add($DestinationFolder.Url + "/" +$file.Name, $file.OpenBinary(), $file.Properties, $true)    
                           $Properties = "Author", "Created", "Editor", "Modified", "PublishingPageLayout"
                          
                           Copy-MetaData -sourceItem $file.Item -DestinationItem $destinationFile.Item -Properties $Properties
                     }
                     else
                     {
                           $SourceFile = $SourceWeb.Site.Url + $SourceLibrary.ServerRelativeUrl + "/" + $file.Name
                           $Destination = $DestinationWeb.Site.Url + $DestinationFolder.ServerRelativeUrl
                           Write-Output "$SourceFile already exists in $Destination"
                     }
              }
       }
}

function Copy-MetaData($sourceItem, $DestinationItem, $Properties)
{
       foreach($property in $Properties)
       {
              if($sourceItem.Fields.ContainsField($property))
              {
                     $DestinationItem[$property] = $sourceItem[$property]
              }     
       }
      
       $DestinationItem.UpdateOverwriteVersion()
       $DestinationItem.Systemupdate()  
}

function Check-FileExists($filename, $folder)
{
       $folder.Files | %{
              if($filename -eq $_.Name)
              {
                     return 1
              }     
       }
       return 0
}