User Tools

Site Tools


notes:sharepoint2010:masterpage

Master Pages in SharePoint 2010

In SharePoint the same master page is used for built-in dialog boxes and almost all other pages. In order to mark a section of the master page as not intended to show up in dialog boxes, mark it with s4-notdlg class, for example:

<div style="height:65px;" class="s4-notdlg">
    <table style="width:100%" cellspacing="0" callpadding="0" border="0">
        <tr>
            <td>
                <SharePoint:SPLinkButton runat="server" NavigateUrl="/" id="btnHome">
                    <asp:Image id="imgHome" ImageUrl="/_layouts/images/Home.png" runat="server"/>
                </SharePoint:SPLinkButton>
            </td>
        </tr>
    </table>
</div>

Applying a custom master page MyMasterPage.master in an event receiver of a web-scope feature. The master page is applied during feature activation and retracted during feature deactivation:

public class Feature1EventReceiver : SPFeatureReceiver
{
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        // Get the parent object that represents the scope of the Feature. 
        // As this feature has the web-scope, SPFeature.Parent returns the SPWeb object.
        SPWeb web = properties.Feature.Parent as SPWeb;
        if (web == null) return;
 
        // Apply the custom master page to the site.
        web.MasterUrl = "/_catalogs/masterpage/MyMasterPage.master";
        web.CustomMasterUrl = "/_catalogs/masterpage/MyMasterPage.master";
        web.Update();
    }
 
    public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
    {
        // Get the parent object that represents the scope of the Feature. 
        SPWeb web = properties.Feature.Parent as SPWeb;
        if (web == null) return;
 
        // Apply the standard master page to the site.
        web.MasterUrl = "/_catalogs/masterpage/V4.master";
        web.CustomMasterUrl = "/_catalogs/masterpage/V4.master";
        web.Update();
    }
}

Another example of activating the custom master page is located here.

Remove a master page passed as the parameter masterPageName (e.g. MyPage.master) from the given site collection :

private static void RemoveMasterPage(string masterPageName, SPSite site)
{
    foreach (SPWeb web in site.RootWeb.Webs)
    {
        Console.WriteLine(String.Format("Remove custom master page from {0}", web.Name));
        SPList sublist = web.Lists["Master Page Gallery"];
        SPListItem subitem = sublist.Items.Cast<SPListItem>()
            .Where(it => it.Name == masterPageName).FirstOrDefault();
        if (subitem != null)
        {
            subitem.Delete();
            Console.WriteLine(String.Format("Master page removed from {0}", web.Name));
        }
    }
 
    Console.WriteLine(String.Format("Remove custom master page from {0}", site.RootWeb.Url));
    SPList list = site.RootWeb.Lists["Master Page Gallery"];
    SPListItem item = list.Items.Cast<SPListItem>()
        .Where(it => it.Name == masterPageName).FirstOrDefault();
    if (item != null)
    {
        item.Delete();
        Console.WriteLine(String.Format("Master page removed from {0}", site.RootWeb.Url));
    }
}

Remove the reference to a master page masterPageName in the given site collection (siteUrl):

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
 
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration.Claims;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Utilities;
using Microsoft.SharePoint.Navigation;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Publishing;
 
public void RemoveMasterPageReference(string masterPageName, string siteUrl)
{
    using (SPWeb web = new SPSite(siteUrl).RootWeb)
    {
        SPList list = web.Lists["Master Page Gallery"];
        SPListItem item = list.Items.Cast<SPListItem>()
            .Where(it => it.Name == masterPageName).FirstOrDefault();
 
        if (item == null)
            throw new FileNotFoundException(masterPageName + " not found");
 
        SPFile file = item.File;
 
        Console.WriteLine("FILE: " + file.Name);
 
        foreach (SPLink link in file.BackwardLinks)
        {
            Console.WriteLine("LINK: " + link.Url);
        }
 
        Console.WriteLine("Do you want to continue? (Y/N)");
        string ch = Console.ReadKey(true).KeyChar.ToString().ToUpper();
        if (ch == "Y")
        {
            try
            {
                web.AllowUnsafeUpdates = true;
 
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    Console.WriteLine("1 of 3: Unregister");
                    UnregisterReceivers(list);
 
                    Console.WriteLine("2 of 3: Delete");
                    item.Delete();
 
                    Console.WriteLine("3 of 3: Register");
                    RegisterReceivers(list);
                });
 
            }
            finally
            {
                web.AllowUnsafeUpdates = true;
            }
        }
    }
}
 
private void UnregisterReceivers(SPList list)
{
    SPEventReceiverDefinitionCollection eventReceivers = list.EventReceivers;
    Console.WriteLine("#1: list.EventReceivers.Count=" + list.EventReceivers.Count);
 
    Type type = typeof(Microsoft.SharePoint.Publishing.Internal.BlockDeletionOfInUseItemsEventReceiver);
    Console.WriteLine("#2: type=" + type.ToString());
 
    string str = type.Assembly.FullName.ToString();
    Console.WriteLine("#3: assembly=" + str);
 
    string fullName = type.FullName;
    Console.WriteLine("#4: fullName=" + fullName);
 
    for (int i = eventReceivers.Count - 1; i >= 0; i--)
    {
        SPEventReceiverDefinition definition = eventReceivers[i];
        Console.WriteLine("#5: definition");
        Console.WriteLine("    Assembly=" + definition.Assembly);
        Console.WriteLine("    Class=" + definition.Class);
        Console.WriteLine("    Type=" + definition.Type.ToString());
 
        if (((definition.Assembly == str) && (definition.Class == fullName)) && 
            (definition.Type == SPEventReceiverType.ItemDeleting))
        {
            Console.WriteLine("#6: before delete");
            definition.Delete();
            Console.WriteLine("#7: after delete");
        }
    }
}
 
private void RegisterReceivers(SPList list)
{
    SPEventReceiverDefinitionCollection eventReceivers = list.EventReceivers;
    Type type = typeof(Microsoft.SharePoint.Publishing.Internal.BlockDeletionOfInUseItemsEventReceiver);
    string str = type.Assembly.FullName.ToString();
    string fullName = type.FullName;
    foreach (SPEventReceiverDefinition definition in eventReceivers)
    {
        if (((definition.Assembly == str) && (definition.Class == fullName)) && 
            (definition.Type == SPEventReceiverType.ItemDeleting))
        {
            return;
        }
    }
    SPEventReceiverDefinition definition2 = eventReceivers.Add();
    definition2.Name = "BlockDeletionOfInUseItemsEventReceiver (added via OM)";
    definition2.Type = SPEventReceiverType.ItemDeleting;
    definition2.SequenceNumber = 0x3e8;
    definition2.Assembly = str;
    definition2.Class = fullName;
    definition2.Data = "";
    definition2.Filter = "";
    definition2.Update();
}

Hide certain parts of a master page:

// Hide the navigation manager.
ContentPlaceHolder PlaceHolderLeftNavBar = 
    Page.Master.FindControl("PlaceHolderLeftNavBar") as ContentPlaceHolder;
if (PlaceHolderLeftNavBar != null)
{
    SPNavigationManager QuickLaunchNavigationManager = 
        PlaceHolderLeftNavBar.FindControl("QuickLaunchNavigationManager") as SPNavigationManager;
    if (QuickLaunchNavigationManager != null) QuickLaunchNavigationManager.Visible = false;
}
 
// Hide the 'Help' button.
HtmlGenericControl divHelpButton = Page.Master.FindControl("divHelpButton") as HtmlGenericControl;
if (divHelpButton != null) divHelpButton.Visible = false;
notes/sharepoint2010/masterpage.txt · Last modified: 2016/01/21 by admin