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:

c# httpclient The remote certificate is invalid according to the validation procedure: RemoteCertificateNameMismatch

 If we get this error while trying to get http reponse using HttpClient object, it could mean that certificate validation fails for the remo...