Once we created the event receiver we need to override the WebProvisioned() method as below to change the master page.
public override void WebProvisioned(SPWebEventProperties properties)
{
try
{
using (SPWeb currentWeb = properties.Web)
{
currentWeb.AllowUnsafeUpdates = true;
currentWeb.MasterUrl = "/_catalogs/masterpage/MyCustomMaster.master";
currentWeb.CustomMasterUrl = "/_catalogs/masterpage/MyCustomMaster.master";
//You can use the below approach also, but sometimes that did not work.
//currentWeb.CustomMasterUrl = currentWeb.Site.RootWeb.ServerRelativeUrl + "/_catalogs/masterpage/MyHpIndigo2ndLevelMaster.master";
currentWeb.Update();
currentWeb.AllowUnsafeUpdates = false;
}
}
catch (Exception ex)
{
throw;
}
}
Once you will deploy the event receiver, try creating a web site and you will see the default master page will change to the custom master page.