Thursday, March 19, 2015

Powershell script to copy master page to new site collection after upgrade

I have used this powershell script to  copy master page to new site collection after upgrade


$srcUrl = "http://<Source Site Url>"
$destUrl = "http://<Dest Site Url>"

$site = Get-SPSite $srcUrl
$topWeb = Get-SPWeb $site.Url

### Copy master page to the new site collection
$SourceWebURL = $destUrl
$SourceLibraryTitle = "Master Page Gallery" 
$DestinationLibraryTitle = $SourceLibraryTitle
$MasterPageTitle = "My.master"
$sWeb = Get-SPWeb $SourceWebURL
$sList = $sWeb.Lists | ? {$_.BaseType -eq "DocumentLibrary" -and $_.title -eq $SourceLibraryTitle}
$sItem = $sList.Items | ? {$_.Name -eq $MasterPageTitle}

$dList = $topWeb.Lists | ? {$_.BaseType -eq "DocumentLibrary" -and $_.title -eq $DestinationLibraryTitle}

if($sItem -eq $null)
{
write-host -ForegroundColor red "My.maser does not exist at web application root web"
}
else
{
$sBytes = $sItem.File.OpenBinary()
$dFile = $dList.RootFolder.Files.Add($sItem.Name, $sBytes, $true)

$AllFields = $sItem.Fields | ? {!($_.sealed)}

Write-Host -ForegroundColor green "Copying My master page to the new site collection"

foreach($Field in $AllFields)
{
    if($sItem.Properties[$Field.Title])
    {
        if(!($dFile.Properties[$Field.title]))
        {
            $dFile.AddProperty($Field.Title, $sItem.Properties[$Field.Title])
        }
        else
        {
            $dFile.Properties[$Field.Title] = $sItem.Properties[$Field.Title]
        }
    }
}
$dFile.Update()
if ($dFile.CheckOutType -ne "None")
    {
        $dFile.CheckIn("Automatically checked in by Powershell", "MajorCheckIn");
    }
    if ($dFile.Item.Versions[0].Level -ne "Published")
    {
        $dFile.Publish("Automatically published by Powershell");
    }
    if ($dFile.Item.ModerationInformation.Status -ne "Approved")
    {
        $dFile.Approve("Automatically approved by by Powershell");
    }
}


$sWeb.Dispose()
$topWeb.Dispose()

$site.Dispose()

No comments:

SSL Error - The connection for this site is not secure

 After cloning a git repo of dot net framework website and trying to run it all I could see was this error Turns out the fix was to simply e...