Using Session State in SharePoint 2010


SharePoint 2010 uses two services related to session state that have similar names but are separate and have different purposes.

  1. ASP.NET session state may be used on SharePoint 2010 pages. This service is automatically disabled in normal installations of SharePoint 2010; it may be enabled using the instructions below. Once enabled, it appears on the Service Applications page as “SharePoint Server ASP.NET Session State Service.”
  2. The State Service service application is designed for and only available to Office internal components such as InfoPath Forms Services and Visio Services. This service is automatically enabled in typical installations of SharePoint 2010 and is not related to ASP.NET session state. It appears on the Service Applications page as “State Service.”

To enable ASP.NET session state:

  1. Enter the following PowerShell command in the SharePoint 2010 Management Shell window:
    Enable-SPSessionStateService –DefaultProvision
  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"

Guidelines for using SharePoint Server ASP.NET Session State:

  1. This session state may be used by custom code in web parts and other page-hosted controls.
  2. Session state information is stored in a configurable SQL database; this implies the following:
    1. Load-balancer affinity is not required.
    2. Heavy use of session state must be included in database/capacity planning. By default, the session state database is created on the same database server hosting the SharePoint 2010 farm configuration database. This can be changed to place it on another server; this can be done at a later date if necessary. To move it to another SQL server, it should first be disabled, then enabled again with different configuration that points to the other server.
  3. When enabled, Session state is available to the whole 2010 farm. Each web application can choose to enable it for use on ASP.NET pages (enableSessionState="true").
  4. Session state is partitioned using the root URL. This means that different web applications will have different session state objects; a web part can only access the session state object available to it. If host header site collections are used, then different session objects will be used for each site collection (the browser will have multiple session ids in different cookies).

Additional information about enabling session state:

Syntax 1

Syntax 2

Enable-SPSessionStateService

-DefaultProvision

Enable-SPSessionStateService

-DatabaseName <String>

[-DatabaseServer <String>]

[-DatabaseCredentials <PSCredential>]

Additional parameters are not discussed here; use Get-Help Enable-SPSessionStateService –full for more information.

The Enable-SPSessionStateService cmdlet creates a session state database, installs the ASP.NET session state schema, and updates the Web.config files on the farm to turn on the session state service.

If the DefaultProvision form of the command is used, all default settings are used. These are:

  • DatabaseName = “SessionStateService_<GUID>”
  • DatabaseServer = the same database server as the SharePoint 2010 configuration database
  • DatabaseCredentials = Integrated Windows Authentication

If the DatabaseName form of the command is used, then these parameters may be set explicitly. If some of the parameters are not included, they default as above. To set specific credentials for accessing the session state database, note that this allows you to specify a SQL Authentication credential only, not a Windows credential. Then, use the Get-Credential command to create a PSCredential object from a username and password, then use that object as the argument of the DatabaseCredentials parameter.

After this service is enabled, “SharePoint Server ASP.NET Session State Service” will appear on the Service Applications management page.

Comments (10)

  1. Decatec says:

    Will Velocity distributed cache be available for SP2010 ?

  2. John Toews [MSFT] says:

    Nice overview! Two things:

    – It should not be necessary to edit the web.config file after running Enable-SPSessionStateService.

    – I want to re-emphasize that folks understand their application's use of ASP.NET Session State and its affect on the farm. Because session state is loaded on most page navigations and persisted if it has chanaged, depending on the custom application's (page, webpart, etc.) usage model of session state, it can have negative overall effects on the performance of the SharePoint install.

  3. markarend says:

    Decatec, I am not aware of specific plans to create a version of Velocity session state with distributed caching specifically targeted at SharePoint.  SharePoint's ASP.NET Session State is not based on Velocity; it uses a SQL database to maintain state and items are cached on the individual WFE servers.  It's technically possible to deploy Velocity as a session state provider for SharePoint 2010; in this case you would not enable SharePoint's ASP.NET Session State service, but deploy Velocity and configure via web.config.  I have not tested this to see if there are other potential conflicts that may prevent it from working properly, but I don't expect any.  Caveat developor.

    John, great point to emphasize… understand the impact of using Session State on the performance of your application!  Example design choices: the landing page for a portal that serves 100,000 users must be able to support high volumes of traffic, with occasional peaks of 100% or more increase.  In addition, a portal for publishing information should typically be designed for less reliance on session-style information.  Therefore, on such a portal's web application, a sensible design goal is to avoid using session state altogether.  Design other web apps that support communities and collaborative sites to use session state (recalling that session state objects are local to individual web applications).  To store state on the portal site, turn to other mechanisms such as viewstate or cookies.  

    Advice is the same as the general case: choose specific tools and solutions that are most suitable to the problem at hand, including "indirect" considerations such as performance.

  4. JohnnyB says:

    What about Sharepoint Foundation?  The above powershell command does not seem to be available.  How to turn it on then?

  5. Madhawa says:

    JohnnyB,

    Open the powershell from

    Start-> All programs -> Microsoft SharePoint 2010 Products -> SharePoint 2010 Management Shell

    Then scripts should work.

  6. Michael says:

    I get the following when I run this on a Foundation server:

    The term 'Enable-SPSessionStateService' is not recognized as the name of a cmdl

    et, function, script file, or operable program. Check the spelling of the name,

    or if a path was included, verify that the path is correct and try again.

    At line:1 char:29

    + Enable-SPSessionStateService <<<<  -DefaultProvision

       + CategoryInfo          : ObjectNotFound: (Enable-SPSessionStateService:St

      ring) [], CommandNotFoundException

       + FullyQualifiedErrorId : CommandNotFoundException

    Is it not possible to enable session with SP 2010 Foundation?

  7. markarend says:

    Ooh, sorry I don't have time to investigate this any time soon, I hope the community will help!?

  8. Matt says:

    I cannot get this to work in SP 2010 Foundation either. Woks perfectly in SP Server 2010. Where are you community? :)

  9. bmjglamour says:

    I'm having the same issue with Sharepoint Foundation and Enable-SPSessionStateService.  It is not listed with get-command.  I'm running Foundtion in Farm Mode on Windows 7.  Has this feature been blocked because of being a "client-mode" install?

  10. John Toews [MSFT] says:

    The Enable-SPSessionStateService cmdlet is available in MOSS only, for Access Services reporting, and Project Server. For WSS, you'll need to configure manually.