Introduction
This article explains how to add a Logout link to the top right hand menu of a SharePoint Portal Server/WSS 2.0 site just by adding a few lines of javascript.
Background
Sharepoint Portal Server/WSS 2.0 does not provide logout functionality out of the box. The challenge was to incorporate logout functionality without radically rewriting the underlying .aspx pages that Sharepoint uses. This solution just alters one javascript file (OWSBROWS.JS) which is used on every page. Any javascript that is added to this file will affect all pages.
Some assumptions
- You have the SharePoint Portal Server 2003/WSS 2.0 installed and running on your development PC.
Using the code
When you create a Sharepoint Portal/WSS site it creates a virtual directory called _layouts this points to the global layouts folder that can be found at
Drivename:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\60\template\layouts
Any changes that are made to files in this folder will affect all portal sites running on this machine. Unless you wish to alter all the sites it is recommended that you take a copy of the layouts folder and change the virtual directory in IIS to point to this copy.
Located in the layouts folder is the “1033” (or similar depending on your locale) this folder contains a file called OWSBROWS.JS. This file is basically a browser sniffer and is used on every page.
Add the code below into the OWSBROWS.JS
//declare a boolean variable to check if menu has already been altered
var logoutOnLoadDone = false;
//attach to load event
window.attachEvent("onload", new Function("Logout_OnLoad();"));
//handles addition of "Logout" link to top banner
function Logout_OnLoad()
{
try
{
//check if already altered
if (!logoutOnLoadDone)
{
//get all table cells
var tableCellCol = document.getElementsByTagName("Td");
//look for "ms-banner" class - should only be one
for (var i=0; i<tableCellCol.length; i++)
{
var cellElm = tableCellCol(i);
//if match found...
if (cellElm.className == "ms-banner")
{
//add link for "Logout"
cellElm.innerHTML += " <a href=javascript:Logout()>Logout</a>";
break;
}
}
//set altered flag to true
logoutOnLoadDone = true;
}
}
catch(e)
{
//do nothing - if it doesn't work then no logout appears
}
}
///ogout functionality
function Logout()
{
try
{
if (browseris.ie6up)
{
document.execCommand("ClearAuthenticationCache", "false");
self.location.href = ".";
}
else
{
alert("This feature requires Internet Explorer 6.0 Service Pack 1 or above. " +
"Please close all browser windows in order to complete the logout process.");
}
}
catch (e)
{
alert("This feature requires Internet Explorer 6.0 Service Pack 1 or above. " +
"Please close all browser windows in order to complete the logout process.");
}
}
The script attaches an event handler (Logout_OnLoad) to the windows load event. This will fire every time the page loads. Logout_OnLoad uses the document object model to find all the table cells on the page. It then loops through the collection and finds the cell that has been created by PageHeader control. This has a class name of “ms-banner”. The innerHTML of this cell is appended with a “Logout” link that will fire the Logout function when clicked. The Logout using an IE6 SP1+ only execCommand statement to clear the credentials cache thus forcing a login dialog to appear.
Note:
- You may have to refresh your browser to make the logout appear the first time after altering the javascript.
- This solution will only work for IE6 users that have SP1 and above installed.
- Since Sharepoint using Windows Authentication only external users will see a login prompt, internal users who have access to the site will be automatically logged in again. If you require that internal users are logged out then you will have to change the authentication method from Windows to Basic in IIS or you can change the browser settings to ask for credentials to internal users (in your browser go to Tools -> Internet Options -> Select Security -> Custom Level, then select “Prompt for username and Password” under User Authentication).
Please do let me know if you find any bugs or you have any suggestions to improve the code