I was working on a SharePoint DoD project, due to security requriements(STIG) it needed to display a disclaimer notice banner when a user initiates a session with the SharePoint Site. This solution tells how to customize and deploy the SharePoint Global.asax that triggers the new session start event to display the disclaimer notice banner.

This solution was split into two SharePoint projects because of following reasons:

  • DLL must be installed in the web-application /bin directory to be accessible by the global.asax SharePoint page that triggers the new session event. (Project: DoDNotice)
  • DLL must be installed in the GAC to work properly as a web-application feature. (Project : DoDNoticeActivator)

1. DoDNotice Project: Customize the SharePoint Global.asax to display disclaimer notice banner on a new Session Start

  • Create a new SharePoint project “DoDNotice”. Right click on “DoDNotice” project and select properties.
  • Set the project “Assembly Deployment Package” property to “WebApplication“.
  • Add the SharePoint Mapped folder “{SharePointRoot}\Template\Layouts”.
  • Create a new aspx file name it “Banner.aspx” in “\Template\Layouts” folder. (This is the page that shows the logon banner message.)
  • Add the following code to “Banner.aspx” file.
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Welcome</title>

<style type=”text/css”>

body {

background-color: #133950;

color: black;

font-family: Arial,Helvetica,Tahoma,sans-serif;

font-size: 0.9em;

margin: 0;

text-align: left;

}

.notice {

background-color: white;

margin: 2em;

padding: 1em;

}

.notice div { text-align: center; }

</style>

 

</head>

<body>

<div>

<div>

<script type=”text/javascript”>

/* <![CDATA[ */

var params = {};

(function () {

var e,

a = /\\+/g,

r = /([^&=]+)=?([^&]*)/g,

d = function (s) { return decodeURIComponent(s.replace(a, “”)); },

q = window.location.search.substring(1);

while (e = r.exec(q)) {

params[d(e[1])] = d(e[2]);

}

})();

var uri;

if (‘uri’ in params) {

uri = params[‘uri’];

} else {

uri = ‘/’;

}

var warning = \”<h2>DOD NOTICE AND CONSENT BANNER</h2><p>Your Message</p>”;

function setBannerCookie() {

window.location=uri;

}

document.write(warning);

document.write(‘<div><button type=\”button\” style=\”width:50px;\” onclick=\”return setBannerCookie();\”>OK</button></div>’);

/* ]]> */

</script>

<noscript>

<p>

This site requires Javascript and cookies for access.</p>

</noscript>

</div>

</div>

</body></html>

  • Add a file “Global.asax” in “\Template\Layouts” folder.
  • Add the following code in Global.asax.cs: This code redirects to “Banner.aspx” page when a new Session starts.

protected void Session_Start(object sender, EventArgs e) {

// Code that runs when a new session is started

string requestUrl = Request.Url.ToString();

string baseUrl = Request.Url.GetLeftPart(UriPartial.Authority) + “/_layouts/Banner.aspx?uri=”;

if (!requestUrl.Contains(“uri”))

Response.Redirect(baseUrl + requestUrl);

else

Response.Redirect(requestUrl); }

  • Deploy the solution.
  • You should see a “Banner.aspx” and “Global.asax” files in HIVE under “SharePointRoot}\Template\Layouts” folder.
2. DoDNoticeActivator Project: Deploy the custom global.asax file using the Sharepoint WSP package and Feature activation
  • Create a new SharePoint project “DoDNoticeActivator”. Right click on “DoDNoticeActivator” and select properties.
  • Set the project “Assembly Deployment Package” property to “GlobalAssemblyCache”.
  • Right click the Feature folder and add a new Feature “ActivateDoDBanner”.
  • In “ActivateDoDBanner” feature select the Scope “WebApplication”. The feature xml file should look like this:
<?xml version="1.0" encoding="utf-8" ?>

<Feature xmlns=”http://schemas.microsoft.com/sharepoint/”

Description=”This feature adds the custom global.asax to web application virtual directory and take the backup of the original global.asax file.”

Id=”602D09E4-4F0D-4058-951D-55059BA87943″

Scope=”WebApplication”

Hidden=”False”

Title=”Activate DoD Banner/Notice”

Version=”1.0.0.0″

ReceiverAssembly=”DoDNoticeActivator, Version=1.0.0.0, Culture=neutral, PublicKeyToken=88ce7fc8bcece008″

ReceiverClass=”DoDNoticeActivator.ActivateDoDBanner”>

</Feature>

  • Feature declares a FeatureReciever that needs to be coded to copy the custom ‘Global.asax’ file to the root directory of site that servers incoming traffic from internet (Internet Zone).
  • Right click on “ActivateDoDBanner” feature and Add Feature Recevier. Here is the FeatureActivated and FeatureDeactivating code:

public override void FeatureActivated(SPFeatureReceiverProperties properties)

{

SPWebApplication webApp = (SPWebApplication)properties.Feature.Parent;

try

{

// loop through each IisSettings dictionary pair (zones [dafault, intranet, internet, etc.] configured for web application)

foreach (KeyValuePair<SPUrlZone, SPIisSettings> pair in webApp.IisSettings)

{

// Deploy the customize global.asax to hive and take the back up of sharepoint original global.asax.

string srcFileinfo = Microsoft.SharePoint.Utilities.SPUtility.GetGenericSetupPath(@”TEMPLATE\LAYOUTS\Global.asax”);

string destFileinfo = pair.Value.Path.FullName.ToString() + “\\global.asax”;

 

if (File.Exists(destFileinfo.ToString())

&& (!File.Exists(destFileinfo + “.original”)))

{

// Take the backup of original global.asax . This will happen first time

File.Copy(destFileinfo.ToString(), destFileinfo.ToString() + “.original”, true);

}

if (File.Exists(destFileinfo.ToString())

&& (!File.Exists(destFileinfo + “.hivebackup”)))

{

// Take the backup of original global asax.

File.Copy(destFileinfo.ToString(), destFileinfo.ToString() + “.hivebackup”, true);

// Copy the customize global.asax to hive.

File.Copy(srcFileinfo.ToString(), destFileinfo.ToString(), true);

}

}

}

catch (Exception ex)

{

throw new Exception(“Feature Activation error:” + ex.Message);

}

}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)

{

SPWebApplication webApp = (SPWebApplication)properties.Feature.Parent;

// loop through each IisSettings dictionary pair (zones [dafault, intranet, internet, etc.] configured for web application)

try

{

foreach (KeyValuePair<SPUrlZone, SPIisSettings> pair in webApp.IisSettings)

{

// Restore the Original sharepoint global.asax in hive and delete the back up copy of global.asax

string destFileinfo = pair.Value.Path.FullName.ToString() + “\\global.asax”;

//RemoveFromHive(properties, destFileinfo);

if (File.Exists(destFileinfo + “.hivebackup”))

{

// make the copy of current global asax.

File.Copy(destFileinfo, destFileinfo + “.custom”, true);

 

// copy hivebackup file back to the hive and make that the current global asax.

File.Copy(destFileinfo + “.hivebackup”, destFileinfo.ToString(), true);

File.Delete(destFileinfo + “.hivebackup”);

}

}

}

catch (Exception ex)

{

throw new Exception(“Feature Deactivation error:” + ex.Message);

}

}

 

  • Deploy the project “DoDNoticeActivator” to SharePoint server.
  • Go to SharePoint Central Admin > Manage Web Applications > Select Web Application > Manage Features > Activate the “ActivateDoDBanner” feature.
  • Go to web application virtual directory “C:\inetpub\wwwroot\wss\VirtualDirectories\{web application port}\
  • Verify that the custom global.asax, global.asax.original, global.asax.hivebackup files are there.

Enjoy the disclaimer notice banner on your Sharepoint site on a new Session Start.

 

5 thoughts to “How to display a logon/disclaimer notice banner in SharePoint by customizing the Global.asax and deploy the global.asax file using the Sharepoint WSP.

  • Keith

    Hi. Thanks for posting this. Its really useful, but I’m running into errors with the SPFeature section. I’m using .net 4.5.2, did you use a different version and could that be the issue? For example, on line 24 the SPWebApplication webapp = (SPWebApplication)properties.Future.Parent; is giving me an error on the SPWebapplication “The type or namespace name can not be found”

    Could you assist?

    Thanks,
    Keith

    Reply
  • Rich Mills

    Good catch, Keith. As you probably noticed, this was written for a much older version of SharePoint and .NET framework. In looking at the code, it’s just looking for the Web App that owns the Feature for the banner. (I notice you mistyped it as “Future.” I’m assuming that was a typo.) I think the way we found this the first time was by using Visual Studio to step through the “feature” elements interface and figure out where the Web App reference is actually stored. I haven’t looked at it, but it would not surprise me if the newer .NET interfaces shield or marshal that in some way.

    Reply
  • Matthew Peltzer

    I realize this post is 6 years old, but it seems to be the most up to date info on creating a DoD Notice as required by STIGs.

    I’m trying to get this setup on an internal SharePoint farm. I’m confused by the first part of the first step “Create a new SharePoint project”. Is this done in SharePoint? Visual Studio? Do you have a project shell you could provide as a download?

    Reply
  • D Wilkins

    This is along the lines of what I would like to do in our intranet, too. My thought is by doing it for each session, as long as the user is active, they won’t be interrupted with a new banner. If they’re inactive for a period of time and their session times out, then they need to be re-prompted. I believe it meets the STIG requirements, but minimizes annoyance of the users.

    I, too, would appreciate a project download to get started.

    Reply
  • J Wilson

    The project is created within Visual Studio. To build this *and* be compliant with MS licensing (building it for a government system) you need Visual Studio Pro or higher version.

    Reply

Leave a comment

Your email address will not be published. Required fields are marked *

X