Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I've created a Sharepoint 2010 Visual Webpart in Visual Studio 2012. The structure of the template that it generates is slightly different than in VS 2010. Previously, it would create the Webpart class and a user control. In 2012 that is all combined into a single class file.

However, I've yet to be successfully add any properties to the webpart. It deploys and compiles. But properties aren't showing up in the user interface when you edit the webpart. I've defined it as below:

private string _myString;
    [Category("Custom Properties")]
    [DefaultValue("test123")]
    [Browsable(true)]
    [WebPartStorage(Storage.Personal)]
    [FriendlyNameAttribute("Custom String")]
    [Description("Type a string value.")]
    [XmlElement(ElementName = "MyString1231123")]
    public string MyString
    {
        get
        {
            return _myString;
        }
        set
        {
            _myString = value;
        }
    }

Any issues?

I did notice that the template that vs2012 generates uses a System.Web.UI.WebControls.WebPArts.Webpart based class, not a sharepoint one.

share|improve this question
add comment

2 Answers 2

up vote 0 down vote accepted

try this:

private string _myString;

[Personalizable(PersonalizationScope.Shared), WebBrowsable(true),
WebDisplayName("MyString1231123"),
WebDescription("Type a string value.")]
public string MyString
{
    get
    {
        return _myString;
    }
    set
    {
        _myString = value;
    }
}

in 2012 settings are slightly different :)

http://msdn.microsoft.com/en-gb/library/vstudio/ee231551(v=vs.110).aspx

share|improve this answer
    
Thank you. This worked perfectly. –  Kamil K Jan 22 '13 at 17:45
add comment

You have to declare the Personalizable type [Personalizable(PersonalizationScope.Shared)] or [Personalizable(PersonalizationScope.User)]

So your WebPart property would look like this:

    [WebBrowsable(true)]
    [Personalizable(PersonalizationScope.Shared)]
    [WebDisplayName("My string")]
    [Description("This is my string")]
    [Category("My own Category")]
    public string MyString { get; set; }
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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