I use session state in some of my layout pages that are running a lot of custom code.
I have some solutions that track the cash flow of our companies, and also receive finance reports.
To keep track of some global variables that are user specific I use session state.
In SharePoint 2010 this is disabled per default due to performance degradation risks.
The error message you will get is this one:
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 \\ section in the application configuration
I wanted to turn it back on, and luckily it wasn’t very difficult.
There are 2 different ways to do this depending if you are using SharePoint Server 2010 or
just the Foundation.
1: SharePoint Server 2010
Run the following command from the SharePoint PowerShell console. Enter your own databaseserver and databasename.
Enable-SPSessionStateService -DatabaseServer yourserver -DatabaseName yourpreferredname -SessionTimeout 30
The cmdlet adds a module to all sharepoint web application web.config files (see below)
This will make sure you are able to activate session state, but you still need to turn it on in the web.config file of each web application.
Find the <pages> tag and set enableSessionState to true
2: SharePoint Foundation
Add the following module to the modules section in the web applications web.config file.
<modules>
<add name=”Session” type=”System.Web.SessionState.SessionStateModule” />
</modules>
Then change the enableSessionState to true in the <pages directive just as with SharePoint server.
Next you need to add the module to IIS, and it is done like this is IIS 7.
- Open up your IIS manager.
- Select the Web Application you wish to enable Session state for
- Double click the Modules icon
- Click “Add Managed Module” link in the right hand menu
- Type any name for it, though Session State could be a good name
- Select “System.Web.SessionState.SessionStateModule, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a” from the dropdown list.
Repeat for all web applications if needed.
You might need to do an IIS reset af this, but then session state should be working