Monday, October 15, 2012

JavaScript to add a global link near Site Actions menu in SharePoint 2010

The goal is to add a link next to the Site Actions menu that would always lead back to the portal home, similar to below.




Using IE Developer Tools, I was able to determine the location where I wanted to place my link was inside of the RibbonContainer-TabRowLeft element.

You can modify the master page and inject the following javascript to achieve this:

Read my following post to see how to make this work on all site collections across a web application by using a delegate control!




ExecuteOrDelayUntilScriptLoaded(ExecuteDefaultLoad, "sp.js");

function ExecuteDefaultLoad()
{

 var isWikiEdit = false;
 if ( document.forms[MSOWebPartPageFormName]._wikiPageMode != null )
 {
  if( document.forms[MSOWebPartPageFormName]._wikiPageMode.value == "Edit" )
    {
     isWikiEdit = true;
    }
  }

 var inDesignMode = document.forms[MSOWebPartPageFormName].MSOLayout_InDesignMode.value;
 if (inDesignMode == "1" || isWikiEdit) 
 {  
  // this page is currently in edit mode 
 } 
 else 
 {  
  AddHomeLink();
 } 
}


function AddHomeLink()
{
 var ribbonContainerRowLeft = document.getElementById("RibbonContainer-TabRowLeft");
 if( ribbonContainerRowLeft != null )
 {
  if( ribbonContainerRowLeft.children != null && ribbonContainerRowLeft.children[0] != null )
  {  
  var newSpan = document.createElement("span");
  newSpan.innerHTML='<a class="ms-menu-a" style="cursor:pointer;white-space:nowrap;"    href="javascript:;" title="SharePoint Portal Home" onclick="window.location=\'/\';return false;"><img src="/_layouts/images/hhome.png"  border="0px"/></a>';
  newSpan.className = 'ms-SPLink ms-SpLinkButtonInActive ms-welcomeMenu';
  newSpan.onmouseover= function() {  this.className = "ms-SPLink ms-SpLinkButtonActive ms-welcomeMenu"}; 
  newSpan.onmouseout= function() {  this.className = "ms-SPLink ms-SpLinkButtonInActive ms-welcomeMenu"}; 
  ribbonContainerRowLeft.insertBefore(newSpan, ribbonContainerRowLeft.children[0]);
  }
 }
}


1 comment:

  1. Hi there,
    can I add this script to CEWP instead to achieve this?
    Thanks,
    Anant

    ReplyDelete