I'm loading a page with WebPart in it, and I get NullReferenceException in this line:

HttpContext.Current.Session["Foo"] = value;

because Session is null. The web.config under inetpub folder has

<pages enableSessionState="false" ... >

Does session bag being null is caused by this setting in web.config, or is the cause completely different?


Yes, If you want to use the session-state, you should enable it in the web.config of the webapplication.

To enable ASP.NET session state:

  1. Enter the following PowerShell command in the SharePoint 2010 Management Shell window: Enable-SPSessionStateService –DefaultProvision
    or more advanced:
    Enable-SPSessionStateService -DatabaseName [-DatabaseServer ] [-DatabaseCredentials ]
  2. On each web application for which you want to use session state, edit the web.config file and set the enableSessionState property of the pages element as follows:
    < pages enableSessionState="true"
| improve this answer | |
  • 1
    This solution works 100%, verified. – Frederik P. Apr 26 '12 at 10:36
  • run Sharepoint Powershell as admin of cource – Iman Sep 2 '15 at 8:21
  • I have a SharePoint site where I use HttpContext.Current.Session["Foo"] = value; But pages enableSessionState is set to false. But it does work. What does the pages enableSessionState="true" actually do? – Danny Mar 13 '18 at 8:11

To use session state in SharePoint see Using Session State in SharePoint 2010, but think hard about if you really want to do it.

| improve this answer | |
  • Why should I think hard about it? are there any drawbacks? – itsho Feb 16 '15 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.