Home
Manage Your Code
Snippet: Sharepoint: change current site collection's master page (C#)
Title: Sharepoint: change current site collection's master page Language: C#
Description: Change master page programmatically Views: 338
Author: Jukka Kotamäki Date Added: 11/12/2008
Copy Code  
1var newMaster = properties.Feature.Properties["MasterName"].Value;
2
3			    if (!string.IsNullOrEmpty(newMaster))
4				{
5					using (var curSite = (SPSite)properties.Feature.Parent)
6					{
7                        using (var curWeb = curSite.OpenWeb(""))
8                        {
9                            //got the current site and root web in site, now set the master Url
10                            //to our master page that should have been uploaded as part 
11                            //of our feature
12                            if (curWeb.MasterUrl.Contains("default.master"))
13                            {
14                                curWeb.MasterUrl = curWeb.MasterUrl.Replace("default.master", newMaster);
15                                curWeb.Update();
16                                UpdateLog("MasterUrl property updated for web " + curWeb.Title,
17                                    EventLogEntryType.Information);
18                            }
19
20                        }
21					}
22				}