To set custom sub site icon in a custom master page.

I.e.

For Sub Site – A:

siteA .

For Sub Site – B:

siteB

Below is the approach followed from me:

  1. Added below entry in master page but it was always resulting in wrong URL (404 URL not found) and referring to Site Collection URL (It was needed to refer sub site URL)
<asp:Image runat=”server” id=”idMainSubSiteImageLink” CssClass=”subSiteIconData” ImageUrl=”../../SiteAssets/banner.png”></asp:Image>
  • 2    I have used trick to resolve correct URL by following steps below:
  • -Added entry of View All Site Contents from SharePoint, It’s NavigateUrl part gets converted to href with correct sub site URL.
<SharePoint:SPLinkButton id=”idNavLinkViewAll1123″ CssClass=”subSiteIcon”  runat=”server” NavigateUrl=”~site/SiteAssets/banner.png” Text=””/>
  • -Added below JavaScript to apply correct URL to Image link mentioned above and made SPLink hidden
<script type=”text/javascript”>_spBodyOnLoadFunctionNames.push(“setImageSrc”);function setImageSrc()

{

var tempImageSrc = document.getElementById(“ctl00_idNavLinkViewAll1123”).getAttribute(“href”);

document.getElementById(“ctl00_idMainSubSiteImageLink”).

setAttribute(“src”,tempImageSrc);

}

</script>

<style type=”text/css”>

.subSiteIcon {display:none;}

</style>

  • – Now, it automatically applies proper image as per sub site user is navigating on.