Monday, February 4, 2013

SharePoint 2013 : PowerShell : Changing master page


# SharePoint cmdlets
cls
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host ""
Write-Progress -Activity "Setting up environment" -Status "Loading Modules"
Write-Host "Setting up environment....." -foregroundcolor "Cyan"
if(!(Get-PSSnapin Microsoft.SharePoint.PowerShell -ea 0)) 
{ 
    Write-Progress -Activity "Loading Modules" -Status "Loading Microsoft.SharePoint.PowerShell" 
    Write-Host -NoNewline "Loading Modules...." -foregroundcolor "Cyan"
    Add-PsSnapin Microsoft.SharePoint.PowerShell 
    Write-Host "Done." -ForegroundColor Green
} 
Write-Host -NoNewline "Loading Assemblies...." -foregroundcolor "Cyan"
[void][system.reflection.assembly]::loadfrom("C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint\v4.0_15.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.dll")
Write-Host "Done." -ForegroundColor Green
Write-Progress -Activity "Setting up environment" -Status "Setting up Variables ...."
Write-Host -NoNewline "Setting up Variables...." -foregroundcolor "Cyan"
# Call the Function to setup environment and variables
SetupEnvironment
Write-Host "Done." -ForegroundColor Green

# ValidateURL $SiteCollectionURL
if ($WebApp -eq $null){
    Write-Host "Could not get access to "$WebApp.Url.ToString()".Please contact Administrator." -ForegroundColor Red
    Break;
}

# Check if Web Application is a null object
Write-Host "Web Application Url:"$WebApp.Url.ToString()

# Change master page in each site 
foreach ($Site in $WebApp.sites){

    if ($Site -ne $null){
        $SiteUrl = $Site.Url.TrimEnd("/")
        $SiteWeb = $Site.OpenWeb()

        Write-Host "Current site url:"$SiteUrl

        $MasterPageUrl = "/_catalogs/masterpage/companyfolder/mycompany.master"
    
        $SiteWeb.MasterUrl = $MasterPageUrl
        $SiteWeb.CustomMasterUrl = $MasterPageUrl
        $SiteWeb.Update()
        $SiteWeb.Dispose()
    }
}

No comments:

Post a Comment