Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

What i am trying to acchieve is to write a script that will automatically update all master pages on all site collections in a certain web application.

The only problem i am having is that when i have uploaded the master page to the master page gallery, it is not yet checked in.

I would like yo check in the masterpage from powershell so everyone can see it.

Here is some code to demonstrate, this is not all ofcourse:

$web = $SPSite.OpenWeb();

$MasterPagelist = ($web).Lists |? {$_.Title -eq "Master Page Gallery"}
#Try to upload the master page. 
try
{
    $MasterPagelist.RootFolder.Files.Add($MasterPageName, $MasterPageBytes) | out-null; 
    if($debug){Write-Host "Added master page to root folder of master page gallery..." -foregroundcolor Green}
}
catch
{
    Write-Host "Error : $_" -foregroundcolor Red
    #exit
}

$item = $MasterPagelist.items |? {$_.Name -eq $MasterPageName}

#Set the master page to te wanted item. 
foreach ($website in $SPSite.AllWebs) 
{ 
    $website.CustomMasterUrl = "/" + $item.url
    $website.MasterUrl = "/" + $item.url
    if($debug){Write-Host "Set the master page of" $website.Url "to" $MasterPageName -foregroundcolor Green}

    $website.Update()
}

after setting it this way, everything works for the user i set it from but when i type:

$masterpage.HasPublishedVersion

I get false.

Also no one can view the page except for me.

So my question, how do i check in(Major Version) this file from powershell?

share|improve this question
    
Your approch is working and maybe there is a reason doing it this way. can you please give us a reason why your ar not using a sharepoint solution (WSP) to do this? In generall this would be the best practice. Not saying you are doing it wrong. i'm just curious why you went with this approch. –  Marco Scheel Aug 10 '11 at 14:31
add comment

2 Answers

up vote 2 down vote accepted

You need to CheckIn and Approve your masterpage file after adding. Just after $item = $MasterPagelist.items |? {$_.Name -eq $MasterPageName} add following lines:

$item.File.CheckIn("CheckIn by PowerShell script")
$item.File.Approve("Approve by PowerShell script")
share|improve this answer
    
I forgot the .file so this is the answer thank you –  nldev Aug 10 '11 at 12:29
add comment

You could call the CheckIn method on the file you just added.

$MasterPageFile = $MasterPagelist.RootFolder.Files.Add($MasterPageName, $MasterPageBytes)
$MasterPageFile.CheckIn("Checkin by Powershell")
$MasterPageFile.Publish("Published by Powershell")

I haven't tested the code, but that should probably be sufficient.

share|improve this answer
    
Thanks, the $MasterPageFile does not support the function CheckIn however... It gives an error that the file does not contain the method CheckIn.. –  nldev Aug 10 '11 at 10:51
    
Did you remove the '| out-null' from the line where you Add the masterpage to the Files? –  Remco Eissing Aug 10 '11 at 11:09
    
No i did not, why do i need to remove the out null? out of curiousity? Because what you said is almost right, but the thin i forgot was the $masterpagefile---> .File.checkin() –  nldev Aug 10 '11 at 12:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.