How to: Enable Session State in SharePoint 2010

By default the Session State in SharePoint 2010 is disabled.

The HttpContext.Current.Session will be null in this case and the following code will fail with a null exception:

public static IAPIProfile SessionProfile 
{ 
    get 
    { 
        return (IAPIProfile) HttpContext.Current.Session[Constants.PROFILE_KEY]; 
    } 
    set 
    { 
        HttpContext.Current.Session[Constants.PROFILE_KEY] = value; 
    } 
}
public static IAPIProfile SessionProfile 
{ 
    get 
    { 
        return (IAPIProfile) HttpContext.Current.Session[Constants.PROFILE_KEY]; 
    } 
    set 
    { 
        HttpContext.Current.Session[Constants.PROFILE_KEY] = value; 
    } 
}

 

Enabling the Session State

Locate your Web App’s folder (c:\inetpub\wwwroot\wss\VirtualDirectories\80 or something similar)

Edit web.config, look for the <httpModules> section, add the Session module

<httpModules> 
<add name="Session" type="System.Web.SessionState.SessionStateModule"/> 
</httpModules>
<httpModules> 
<add name="Session" type="System.Web.SessionState.SessionStateModule"/> 
</httpModules>

 

and save.

Then open Server Manager or IIS Manager, browse to your SharePoint Web Application. In features look for “Modules”,

Click on Add Managed Module,

Enter “Session” as the name.

Select “System.Web.SessionStateModule” from the dropdown and save.