Lars Nielsen's Discoveries

October 1, 2012

Session State problems with SharePoint custom pages

Filed under: Development,SharePoint — Lars Nielsen @ 7:37 pm
Tags: , ,

It’s a common requirement to host or embed web applications within an Intranet in SharePoint. The quick-but-dirty solution to this is to use a Page Viewer Web Part or a custom HTML page that displays an external ASPX page within a SharePoint page inside an IFRAME. The problem with this is what happens when the content within the IFRAME changes – you’ll see either a big patch of white space in the frame (if it shrinks) or an embedded scrollbar (if it grows) – neither of which is very nice for end users.

But because SharePoint is itself written in .NET, you can do something more clever and you can create ASPX pages in a SharePoint site that reference exactly the same master page and branding as the site itself. I use this technique to create web applications outside of SharePoint and then copy the ASPX pages over into a SharePoint project so that they can be provisioned into a SharePoint site via feature activation. This has the neat advantage that a single application can be deployed several times into different SharePoint sites, simply by activating the feature on each site. And because SharePoint master pages are dynamic (if you have the Publishing features activated), the same ASP.NET web application can even be branded differently on different sites.

The original great articles by Andrew Connell describe this technique very well here  and here. Although these are rather old now, the same technique can be adapted to SharePoint 2010 and Visual Studio 2010.

Recently when I was working on this I came across a problem when trying to use Session State in the code behind to persist a value between 2 different pages. Everything worked fine outside SharePoint, in a native ASP.NET web application. But when I migrated the pages into a SharePoint site, and tried them, I got this error:

Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive. Please also make sure that System.Web.SessionStateModule or a custom session state module is included in the \<system.web>\ section in the application configuration.

I checked to see that session state was enabled in SharePoint and had a quick look at the web.config file for the web application. In the web.config there is a line in – <system.web> like this:

<pages … enableSessionState=”false” … />

So I thought that turns off session state for all ASPX pages by default. To use session state on my pages I added to the Page directive an attribute to turn on session state:

<%@ Page EnableSessionState=”true” … %>

I refreshed the main page and saw this error instead:

The enablesessionstate attribute on the page directive is not allowed in this page

At this point I looked around the web and found this discussion.  But I didn’t want to mess around with the PageParserPaths element in the web.config. I thought there must be a better way, and found this discussion ( where it seems that resetting the pages to the site definition will fix it.  I tried resetting to the site definition, but it didn’t help.  Then I found this gave me a clue.

I tried changing the master page in my SharePoint site, which was a custom master page, to the out-of-the-box default.master.  Suddenly the error disappeared!  I switched back to my custom master page and the error came back.  I even created my own custom master page which was a copy of the standard default.master page, and applied this to the site, and that copy still caused the same exception.   It seems session state will not work unless the master page is uncustomized.

For the moment I don’t how to get around this and I just design my code assuming that session state is not available, but if anyone has any good ideas how to use session state please let the world know!


May 15, 2012

Down for Maintenance page for SharePoint sites

Filed under: Administration,SharePoint — Lars Nielsen @ 8:36 pm
Tags: , ,

Here’s a quick tip if you need to take a SharePoint site down for maintenance, or if you have some incident that takes it down.  For example, if you’re restoring a site collection, you can put up a friendly “down of maintenance” message when people visit the site.  You can put a “no access” lock on the site collection in Central Admin, but that won’t help if you’re restoring a site collection because it needs to be unlocked.  And when the site collection is deleted, visitors will just get a timeout error in their browser if they try to go to a page within the site collection.

If you can turn off access to a whole web application then there’s a another way.  This might apply for example to an Intranet that has its own web application.  You can put a special file called app_offline.htm in the IIS root folder of the site (the same folder than contains the web.config file).  This file can contain HTML, CSS and Javascript (but no ASP.NET code – it is pure HTML).  We use it to provide links to external applications and services that are not part of SharePoint, but which people frequently access via the Intranet.   In this way people can still find those applications even if the Intranet is down.

When you do this, IIS will return the app_offline.htm for every web requestwithin the web application domain.  So don’t use <script> or <link> tags to reference other files in the same folder.  Keep everything within the HTML code of the page itself.

This is an ASP.NET facility, but it works fine with SharePoint as well.  Because it’s ASP.NET it doesn’t rely on SharePoint at all. So even if your farm is down completely (database server down, for example) then this technique will still work. You need to deploy this file manually on the appropriate web server(s) into the right IIS folder on the file system.

Blog at