Following example shows how to change the master page of SharePoint site on feature activation:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb currWeb = properties.Feature.Parent as SPWeb;
try
{
currWeb.AllowUnsafeUpdates = true;
SPWeb rootWeb = currWeb.Site.RootWeb;
if (rootWeb.GetFile(rootWeb.Url + "/_catalogs/masterpage/NewMaster.master").Exists)
{
Uri masterUri = new Uri(rootWeb.Url + "/_catalogs/masterpage/ NewMaster .master");
currWeb.MasterUrl = masterUri.AbsolutePath;
// Sets application master page
currWeb.CustomMasterUrl = masterUri.AbsolutePath;
currWeb.Update();
}
currWeb.AllowUnsafeUpdates = false;
}
catch (Exception ex)
{
// Log exception
}
}