Monday 28 September 2015

Powershell script to convert .ppt to .pptx in Sharepoint document library

Problem/Issue

Most of the word documents migrated from SharePoint 2007 to SharePoint 2010 have .ppt extension. I want to roll out Office Web Apps to our SharePoint instance. The only trouble is that a lot of files were still in .ppt format, and when we try to edit/open them in Office Web Apps it has to convert them to .pptx first. If we do it manually , it makes a copy of the file and adds (converted) to the filename . we will have to remove the file with .ppt extension and rename the newly copied file which was quite difficult, as we have thousands of documents in each library.

Solution:


I have written a powershell script/ tool to acheive this functionality and it worked like a charm!

The below are the steps , followed with the necessary scripts to achieve this functionality.
Execution Steps:
  • Copy the below files to WFE server
1.    pptfileconversion.bat
2.    pptfileconversion.ps1
  •   Before executing the script we need to install powerpoint  application on the server(WFE) where we are executing the script
  •  Execute the batch file “pptfileconversion.bat”.
  • During its execution it will prompt user to enter site URL, Library Name
For Ex: If we want to convert ppt files in the Library “MigratedImages” which is located in site https://test.com/departments/test, then we need to enter the values as below
 

It will display the list of files that are failed to convert and the ppt file remains as it is.

Note: This document does not lose its original metadata while conversion
This script automatically deletes the original “.ppt” file after conversion.

Code Files
1.    pptfileconversion.bat

cd /d %~dp0

powershell -noexit -file    ".\pptfileconversion.ps1"

pause


2.    pptfileconversion.ps1
Add-PsSnapin Microsoft.SharePoint.PowerShell

# Input parameters for the script
#[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.Office.Interop.PowerPoint')

Add-type -AssemblyName office -ErrorAction SilentlyContinue
Add-Type -AssemblyName microsoft.office.interop.powerpoint -ErrorAction SilentlyContinue


$pptFixedFormat = [Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType]::ppSaveAsOpenXMLPresentation  

$ppt = new-object -com powerpoint.application
try
{
$ppt.visible = [Microsoft.Office.Core.MsoTriState]::msoFalse
}
catch
{
}

#$presentation = New-Object -ComObject PowerPoint.application
#$varvisible = 0

#$presentation.visible = [Microsoft.Office.Core.MsoTriState]::msoFalse


$websiteurl = read-host "Enter site URL"
$web = Get-SPWeb $websiteurl

$LibraryName = read-host "Enter Library Name"

Function CopyMetadata($MetaFile)
{


$searchfile = $MetaFile.file
$searchfileurl = $searchfile.Url
$searchfilename = $searchfileurl+"x"

$folder = $web.GetFolder($LibraryName)
$library = $folder.DocumentLibrary
$spQuery = New-Object Microsoft.SharePoint.SPQuery
$spQuery.ViewAttributes = "Scope='Recursive'"
$spQuery.Query = "<Where><Contains><FieldRef Name='FileLeafRef' /><Value Type='File'>.ppt</Value></Contains></Where>";
$listItems = $library.GetItems($spQuery)
$targetfileItem = $MetaFile
foreach($item in $listItems)
{
$sourcefile = $item.file
$sourcefileurl = $sourcefile.Url

if($sourcefileurl -eq $searchfilename)
{

$item["Author"] = $targetfileItem["Author"]
$item["Editor"] = $targetfileItem["Editor"]
$item["Created"] = $targetfileItem["Created"]
$item["Modified"] = $targetfileItem["Modified"]
$item.Update()
$searchfile.Delete()
}
}

}
Function getallfiles($test)
{
write-host "copying Metadata information and deleting Original File"

$folder = $web.GetFolder($LibraryName)
$library = $folder.DocumentLibrary
$spQuery = New-Object Microsoft.SharePoint.SPQuery
$spQuery.ViewAttributes = "Scope='Recursive'"
$spQuery.Query = "<Where><Contains><FieldRef Name='FileLeafRef' /><Value Type='File'>.ppt</Value></Contains></Where>";

$listItems = $library.GetItems($spQuery)

foreach($item in $listItems)
{
$filename = $item.file
$inputFile = $filename.name
$fileextension = $inputFile.substring($inputFile.lastindexof('.'),($inputFile.length - $inputFile.lastindexof('.')))
if($fileextension -eq ".ppt")
{
CopyMetadata($item)

}
}

}

Function ProcessFolder([string]$folder)
 {
write-host "conversion job started"
$inputFolder = $web.GetFolder($folder)
$library = $inputFolder.DocumentLibrary
$spQuery = New-Object Microsoft.SharePoint.SPQuery
$spQuery.ViewAttributes = "Scope='Recursive'"
$spQuery.Query = "<Where><Contains><FieldRef Name='FileLeafRef' /><Value Type='File'>.ppt</Value></Contains></Where>";

$listItems = $library.GetItems($spQuery)
foreach ($item in $listItems)
{
$fileobj = $item.file
$fileurl = $fileobj.Url
$inputFile = $fileobj.name
$fileextension = $inputFile.substring($inputFile.lastindexof('.'),($inputFile.length - $inputFile.lastindexof('.')))
if($fileextension -eq ".ppt")
{

$outputFile = $inputFile.Replace(".ppt",".pptx")
$inputfileurl = $web.Url+"/"+$fileurl
$outputfileurl = $inputfileurl.Replace(".ppt",".pptx")
#Add input and output folders and start conversion
try
{
write-host -Fore Green "converting file :     $inputfileurl"
$pres = $ppt.Presentations.Open($inputfileurl,$true)
$pres.saveas($outputfileurl, $pptFixedFormat)
$pres.save()
$pres.close()
}
catch [system.exception]
{
   Write-host "Exception occured while converting file : $($_.Exception.Message)"
write-host -Fore Red "File failed to convert : $inputfileurl"
   #do some error handling
}
catch
{
Write-error -Fore Red "Error occured while converting file $inputFile"
}
}
}
}
ProcessFolder($LibraryName)
$ppt.displayalerts = [Microsoft.Office.Interop.PowerPoint.PpAlertLevel]::ppAlertsNone
$ppt.Quit()
$ppt = $null
[gc]::collect()
[gc]::WaitForPendingFinalizers()
getallfiles("welcome")
write-host "Conversion operation completed"
$web.Dispose()
Remove-PsSnapin Microsoft.SharePoint.PowerShell

No comments:

Post a Comment