3

I developed a custom webpart with a webpart property with some textboxes and it works partially.

If I deploy the project, the written text in the property is away. This is ok.

But my MAIN Problem is, that about after one day the text in the property disappear is away. I use SharePoint 2010.

Here is my code:

[WebBrowsable(true), Category("category"), Personalizable(PersonalizationScope.Shared), WebDisplayName("Hello"), WebDescription("Description1")]
    public string hello
    {
        get { return _hello; }
        set { _hello = value; }
    }
    public static string _hello;
grekko
  • 53
  • 1
  • 11
  • What do you mean that the property disappears? The whole property or just the contents of it, so it looses its value? Are you using the same user, tried with a site collection administrator? – Dennis G Aug 21 '12 at 10:49
  • Only the value (text) disappears, which I wrote in the Textboxes. I use the moss-admin user. It is a farm solution and other users cannot enter ti the values, too. – grekko Aug 21 '12 at 11:43

3 Answers3

3

You shouldnt use the static declarator for your _hello string. This value is instance-independent (thus static) and will lose its value when the application pool recycles (which occurs once a day). Remove the static and just use the shorthand and you should be fine:

[WebBrowsable(true),
 Category("category"),
 Personalizable(PersonalizationScope.Shared),
 WebDisplayName("Hello"),
 WebDescription("Description1"),
WebPartStorage(Storage.Shared)] 
public string Hello{ get; set;}
Rikard Uppström
  • 1,413
  • 9
  • 16
  • It sounds good and logical. If I remove static, I get an error: An object reference is required for the non-static field, method, or property 'WebPartProperty.VisualWebPart1.VisualWebPart1._hello'. I use the string _hello in the ascx.cs file, but made the property in VisualWebPart.cs – grekko Aug 21 '12 at 12:24
  • +100 for Rikard. Grekko - You need to do a bit of research to really understand what static means and the differences between classes and class instances - all fairly basic stuff you MUST understand to be able to develop effectively in modern languages such as C#, Java etc. – Ryan Aug 21 '12 at 13:03
0

I solved it.

I have the SharePoint-WebPart-Property defintion in the VisualWebPart1.cs and the build my "main" WebPart in the VisualWebPart1UserControl.ascx.cs.

So the main problem was the connection between UserControl and the VisualWebPart1.cs. Here is the code for "connection" in the VisualWebPart1.cs:

        protected override void CreateChildControls()
    {
        VisualWebPart1UserControl control = (VisualWebPart1UserControl)Page.LoadControl(_ascxPath);
        control.WebPart = this;
        Controls.Add(control);
    }

Then comes the Property WITHOUT static:

public string _hello;
[WebBrowsable(true), Category("category"), Personalizable(PersonalizationScope.Shared), WebDisplayName("Hello"), WebDescription("Description1")]
public string hello
{
    get { return _hello; }
    set { _hello = value; }
}

The next step I did is to get to the UserControl.ascx.cs and define a getter and setter to connect the UserControl.ascx.cs with VisualWebPart1.cs:

public VisualWebPart1 WebPart { get; set; }

Then you can initialize the variable with WebPart.hello

grekko
  • 53
  • 1
  • 11
-1

In all the webparts I have developed, I've used WebPartStorage attribute as well. My guess is that if you don't use it, the property is only stored in memory. Hence, after IISRESET/application pool recycle your property value is lost.

So you could change your code to this:

 [WebBrowsable(true), 
  Category("category"), 
  Personalizable(PersonalizationScope.Shared), 
  WebDisplayName("Hello"), 
  WebDescription("Description1"),
  WebPartStorage(Storage.Shared)] 
  public string hello 
  { 
      get { return _hello; } 
      set { _hello = value; } 
  } 
  public static string _hello; 
naivists
  • 32,681
  • 5
  • 61
  • 85
  • 1
    Never heard of `WebPartStorage` and MSDN doesn't have any references for it for SharePoint 2010 (only for SP2003): http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webpartpages.webpartstorageattribute.aspx – Dennis G Aug 21 '12 at 10:48
  • The page you are linking to is in the SPS2010 branch :-) However, it is true that in SharePoint 2010 it has become redundant, since `Personalizable` attribute is enough (as per discussion here: http://sharepoint.stackexchange.com/questions/12469/webpart-custom-properties) – naivists Aug 21 '12 at 19:49
  • Interesting discussion! But no, it is not the SP2010 branch - look at the "Versions" drop down on top. It only contains *Windows SharePoint Services 3*, in case it would still be supported we would see different versions there AFAIK. – Dennis G Aug 21 '12 at 20:33