ARTICLE

How to set a custom master page to the site in SharePoint 2010

Posted by Vijai Anand Articles | SharePoint 2010 February 28, 2011
In this article we will be seeing how to set the custom master page to the site in SharePoint 2010 using powershell and using console application.
Reader Level:

In this article we will be seeing how to set the custom master page to the site in SharePoint 2010 using powershell and using console application.

In this article, I have created custom master page and it is placed in the physical path. Through script I will be uploading the master page to "Master Page Gallery" and set the master page to the SharePoint site.

Set a master page to the site in SharePoint 2010 using powershell script

# -------------Comments

$publishComment="Published"
$approveComment="Approved"
$checkInComment="Custom Master Page added"



$site=Get-SpSite "http://servername:1111/"
$web=$site.RootWeb
$masterPageLocalDir = "C:\CustomMasterPage.master"
$masterPageFolder = $web.GetFolder("Master Page Gallery")

# -------------Uploading the master page to the "Master Page Gallery"

Get-ChildItem $masterPageLocalDir | foreach {
$stream = [IO.File]::OpenRead($_.FullName)
$desturl = $web.Url+"/_catalogs/masterpage/"+$_.Name.Replace(" ","")
$resultingfile =$masterPageFolder.files.Add($desturl,$stream,$true)
$stream.close()

}

# -------------Check In, Publish and Approve

$masterPageFile=$web.GetFile($desturl)
$masterPageFile.CheckIn($checkInComment);
$masterPageFile.Publish($publishComment);
$masterPageFile.Approve($approveComment);
$masterPageFile.Update();



# -------------Set the master page to the site

$masterUri=New-Object System.Uri($desturl)
$web.MasterUrl=$masterUri.AbsolutePath
$web.CustomMasterUrl=$masterUri.AbsolutePath
$web.Update()
$web.Dispose()
$site.Dispose()


For creating master page using visual studio 2010 refer http://www.c-sharpcorner.com/UploadFile/anavijai/5224/

Login to add your contents and source code to this article
post comment
     

Ya sure will post an article on how to deploy .css files using powershell soon

Posted by Vijai Anand Mar 28, 2011

Vijai, Can you please let me know how to deploy/set custom .css files and images using Powershell?

Posted by Vasu Rao Mar 28, 2011

Thank You:-)

Posted by Vijai Anand Mar 28, 2011

This really helped me a lot as I was checking for the same for the past 2-3 days. Can you please let me know how to deploy/set custom .css files and images also?

Posted by Vasu Rao Mar 28, 2011
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts