none
sharepoint web part pages - no session state - best practice to persist objects between pages ?

    Question

  • Wonder what's the best way to handle persisting objects between pages in sharepoint web part pages?  Session state doesn't work - (though I know you can set it up - but we aren't doing it now and aren't going to apparently)

    I've seen similar questions but not any satisfactory answers.

    Thanks!

    Wednesday, October 28, 2009 5:28 PM

Answers

  • The SPPropertyBag will store objects, but unless you use a unique key per user the stored object for one user will be the same as for every other user, which doesn't really give a "session state" scenario. Plus, as Razi said the property bag will hold on to these objects until you explicity delete them, which will mean you may end up with a lot of garbage attached to your SPWeb.

    If you only have a single front-end, ASP.NET caching may be the way to go. However this won't work if you have multiple front-end in a load balanced config because the cache is not synched across multiple servers.

    How complex is the data you wish to persist across pages? If its not complex data, you could always use good'ol cookies?

    Paul.
    • Marked as answer by dd3141592 Friday, October 30, 2009 10:07 PM
    Thursday, October 29, 2009 3:35 PM

All replies

  • If you decided not to use Session object , basically you then left with rather limited object sets. This is essentially because of fact that SharePoint build on top of ASP.NET framework. Cache object is next that you can utlilize, but really depends on your scenario. In SharePoint you have what is called SPProperty bag that can be used to store values , but this persits the data on perminent basis so you will be responisble for cleaning it up once your work with it is done. Do take note that SPProperty bag does not support complex types.

    SPProperty Bag

    SPPropertyBag Bag =  site.RootWeb.Properties;
    if (!Bag.ContainsKey("PropertyName"))
    {            
          Bag.Add("PropertyName", "PropertyValue");
          Bag.Update();
    }
    SPPropertyBag Bag =  site.RootWeb.Properties;
    if (Bag.ContainsKey("PropertyName"))
    {
        Bag["PropertyName"]=null;                            
        Bag.Update();
    }


    http://razi.spaces.live.com/
    Thursday, October 29, 2009 9:00 AM
  • You can try using viewstate to "store" non-primitive objects
    Never stop learning.
    Thursday, October 29, 2009 9:02 AM
  • Thanks for this info.  I need something that will work per session and this looks like it's on the site level since I see a code example like this:

    SPPropertyBag spProperties = SPContext.Current.Web.Properties;

    Am I misunderstanding?  Nice to know about even if it's not what I need right now.
    Thanks.



    Thursday, October 29, 2009 2:07 PM
  • Thanks, But this doesn't help with separate sharepoint pages does it?  Isn't it a between server trips within a page where values are stored?  I need something to pass between pages. 
    Thursday, October 29, 2009 2:09 PM
  • I haven't tried this but here's a crazy idea that just might work: -

    If you create a custom masterpage for the pages that you want to store the sate between (Let's say for example, you had a three page wizard and you wanted to store state between them, you'd create one master page thats shared by all). You could add hidden fields to the master page, populate them from your code and pick them up in subsequent pages.





    • Edited by Mike Walsh FIN Friday, October 30, 2009 10:57 AM sig removed. Don't ask people to mark your post as answered
    Thursday, October 29, 2009 3:12 PM
  • The SPPropertyBag will store objects, but unless you use a unique key per user the stored object for one user will be the same as for every other user, which doesn't really give a "session state" scenario. Plus, as Razi said the property bag will hold on to these objects until you explicity delete them, which will mean you may end up with a lot of garbage attached to your SPWeb.

    If you only have a single front-end, ASP.NET caching may be the way to go. However this won't work if you have multiple front-end in a load balanced config because the cache is not synched across multiple servers.

    How complex is the data you wish to persist across pages? If its not complex data, you could always use good'ol cookies?

    Paul.
    • Marked as answer by dd3141592 Friday, October 30, 2009 10:07 PM
    Thursday, October 29, 2009 3:35 PM
  • Thanks for the reply - that's probably what I'll do - rely on good'ol cookies.
    Friday, October 30, 2009 10:07 PM
  • That's an interesting idea.  Might try it another time when i don't have master page designers/owners to contend with.
    Friday, October 30, 2009 10:08 PM