I noticed below points while
working with master page
If master page is not customized, I mean if it is
not modified by SharePoint designer or any other tool
2nd time when I am done with
modifications in my feature code, I am updating a master page feature using below powershell script
update-spsolution -identity MasterPages.wsp -literalpath C:\Pulipati\.MasterPages.wsp - gacdeployment
a. My Master page is updating properly however it is not
creating a version and it is not updating the modified date as well when I run
above command. But when I open a master
page I see latest code.
If master page is customized, I mean if it is modified
by SharePoint designer or any other tool
If I run
powershell command it is not updating my master page. So I followed this Post and
successfully updated.
<Module Name="PGCMasterPageModule" List="116" Url="_catalogs/masterpage">
<File Path="xxxMasterPageModule\CustomMaster.master" Url="CustomMaster.master" IgnoreIfAlreadyExists="TRUE" Type="GhostableInLibrary"
/>
</Module>
IgnoreIfAlreadyExists="TRUE": always make this TRUE it will not throw any error when you deploy second
time.
using
System.IO;
public override void FeatureActivated(SPFeatureReceiverProperties
properties)
{
//if you want to debug just enable this code
// System.Diagnostics.Debugger.Break();
SPSite currentSite = (SPSite)properties.Feature.Parent;
SPWeb oCurrentWeb = currentSite.RootWeb;
oCurrentWeb.MasterUrl =
oCurrentWeb.ServerRelativeUrl + "/_catalogs/masterpage/CustomMaster.master";
oCurrentWeb.CustomMasterUrl = oCurrentWeb.ServerRelativeUrl + "/_catalogs/masterpage/CustomMaster.master";
oCurrentWeb.Update();
//below code executes only when the file is customized (for ex: if u modify with sharepoint designer)
if
(oFile.CustomizedPageStatus == SPCustomizedPageStatus.Customized)
{
// Here we are trying to overwrite the default.master
SPFile oFile = oCurrentWeb.GetFile("_catalogs/masterpage/CustomMaster.master");
SPFolder oFolder = oCurrentWeb.Lists["Master Page Gallery"].RootFolder;
// Check In the file
if (oFile.CheckOutType != SPFile.SPCheckOutType.None)
oFile.CheckIn("auto checkin");
// Check Out the file for edit.
oFile.CheckOut();
string directory =
properties.Definition.RootDirectory; // it returns 14 hive feature path
directory += @"\PGCMasterPageModule"; // my feature module name as shown in picture
// Get the master page name from the feature properties as
defined in the elements.xml
// SPFeatureProperty
oPropertyName = properties.Feature.Properties["CustomMaster.master"];
// Get the file names from the specified directory matching
the feature property value.
string[] templates = Directory.GetFiles(directory,
"CustomMaster.master", System.IO.SearchOption.TopDirectoryOnly);
// templates should have all the matching file names. In
this instance it whould be only one file name.
string MasterPageFile = templates[0];
FileInfo
fileInfo = new FileInfo(MasterPageFile);
byte[] byteArr = System.IO.File.ReadAllBytes(MasterPageFile);
oFolder.Files.Add(fileInfo.Name, byteArr, true);
oFile.Update();
oFile.CheckIn("Check in");
oFile.Publish("Published");
oFile.Approve("Approved");
}
}