0

I created a webpart with custom properties but once the webpart is added and the settings changed, it doesn't take effect until a publish or check in share draft. Clicking in OK or Apply in the webpart settings doesn't save the properties.

private string _ListSite="/";
private string _ListName = "Quick Links";

[WebBrowsable(true)]
[WebDisplayName("List site")]
[SPWebCategoryName("Quick Links Settings")]
[WebPartStorage(Storage.Shared)]
[WebDescription("Name of the site where the links list is")]
[Personalizable(PersonalizationScope.Shared)]
public string ListSite
{
   get { return _ListSite; }
   set { _ListSite = value; }
}

[WebBrowsable(true)]
[WebDisplayName("List name")]
[SPWebCategoryName("Quick Links Settings")]
[WebPartStorage(Storage.Shared)]
[WebDescription("Name of the list to use")]
[Personalizable(PersonalizationScope.Shared)]
public string ListName
{
    get { return _ListName; }
    set { _ListName = value; }
}

2 Answers 2

1

Calling the method that needs the custom properties updated in CreateChildControls() has done the job for me.

Example:

protected override void CreateChildControls()
{
    base.CreateChildControls();

    try
    {
        _data = GetLinksData();
    }
    catch (Exception ex)
    {
        //exception code
    }

}

private SPListItemCollection GetLinksData()
{
   //Method that uses the custom properties
   //this.ListSite has the new value when OK or Apply has been clicked in the webpart settings
   //using this.ListSite! 
}
0

Ensure that you derive your webpart from "System.Web.UI.WebControls.WebParts.WebPart" and need not use attributes like "SPWebCategoryName" or "WebPartStorage" .The below attributes are sufficient for a webpart property to work as expected:

[Personalizable(PersonalizationScope.Shared)]
[WebBrowsable(true)]
[Category("Quick Links Settings")]
[WebDisplayName("List site")]  
[Description("Name of the site where the links list is")] 

If, for some reason, you drive from Microsoft.SharePoint.WebPartPages.WebPart and you want save the properties in code, you can use WebPart.SaveProperties as below

   // Set Title property of webpart  to the value in _mytextbox control.
    public void _mybutton_click (object sender, EventArgs e)
    {
        this.Title = _mytextbox.Value;
        try
        {
            this.SaveProperties=true;
        }
        catch
        {
            Caption = "Error... Could not save property.";
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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