views:

103

answers:

1

I'm pretty new to SharePoint Web Part development, so I'm not sure the best practices for handling data persistence. In my application, user postbacks will be modifying some underlying data structure that I would like to maintain rather than rebuild.

The two things I have tried so far are making objects serializable, and storing them in the session array. It seems like there should be a more elegant solution though. I have also tried using custom properties where I set the web browsable attribute to false... although I haven't tried using it for custom classes.

What is the preferred method for data persistence in webparts?

Edit: Here is an example of what I think you are suggesting. First, a class

[Serializable]
    public class foo
    {
        public foo()
        {

        }
    }

Then in my webpart, I would have something like...

public class WebPart : WebControls.WebParts.WebPart
{
    [WebBrowsable(false)]
    [WebPartStorage(Storage.Personal)]
    public foo bar { get; set; }
}

The next problem I seem to have is these values don't seem to persist if I change them programatically. Anything that I expose to the user through the property panel will persist only if the user makes the change. Am I missing something?

+1  A: 

You should take a look: ASP.NET Web Parts Overview and Web Parts Personalization Overview. I personally store webparts configuration by using WebPartStorage attribute on public properties from my webpart class.

By default, sessions are disabled on Sharepoint. You should take extra care if you plan to enable it, including performance and stress tests.

Rubens Farias
Ok I want to go the custom properties route, but I have a problem... see above.
tbischel
I generally store simple values, as text and enums, defined on my toolpart; so, I have no experience on storing complex types, sorry
Rubens Farias