SharePoint Master Page: Include Title of a Site as a link to the Site’s Homepage
When building a Master Page for use on multiple sites across a SharePoint site collections, there is often a need to include the Title of the current site on every page on a site that links back to the homepage of the current site when clicked.
In this article, I demonstrate how to achieve this using a Project Property (Title) and the ~site url token to include a link to the back to the homepage of the current site. When placed in a master page, the visible (anchor) text for the link will include the Title of the current site dynamically, and links back to the homepage of the site. This is applicable to both MOSS (SharePoint 2007) and SharePoint 2010 sites and Master Pages.
The Title of the current site is obtained using the "Title" property, which can be accessed using Project Properties available in the context of a site or sub-site in a site collection:
The following tag will display the Title of the current site:
<SharePoint:ProjectProperty Property="Title" runat=server" />
To create a link to the homepage of the current site, the "~site" token in this case is used to form the base part of the url. As different types of sites can have homepage URLs that vary in structure (eg. a Publishing Site or Site Directory by default doesn’t use the default.aspx page at the root of a site/sub-site as a team site or similar would), the link is created to the root of the site or sub-site to allow the SharePoint to serve the homepage of any site.
The link to the homepage is constructed using the following tag:
<a href="<% $SPUrl:~site/ %>" runat="server">....</a>
To include the Title of the current site as the text displayed in the link, the Title Property can be placed inside the opening and closing <a> tag as follows:
<a href="<% $SPUrl:~site/ %>" runat="server"><SharePoint:ProjectProperty Property="Title" runat=server" /></a>
The result will link to the homepage of the current site with the Title displayed as the text in the hyperlink.
Submit a review:
Login required.