3

I am using SharePoint 2013. I developed a visual webpart in visual studio 2013. I have added this code in the code-behind of my webpart:

public partial class VisualWebPart1UserControl : UserControl
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }

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

    [WebPartStorage(Storage.Shared)]
    [Personalizable(PersonalizationScope.Shared)]
    [FriendlyNameAttribute("Update interval")]
    [Description("Insert feed update interval")]
    [WebBrowsable(true)]
    [Category("Message Configuration")]
    public int UpdateInterval { get; set; }

}

I deploy the webpart. I add the webpart to my homepage. I click on edit webpart and see right above all the categories and properties. But I don't see my custom category "Message Configuration". I miss also my custom property "UpdateInterval" in the existing categories.

Do I miss something in my code? I would like to have only a custom property make something manageable by the site administrators.

enter image description here

I did an export of my webpart in SharePoint. I opened the .webpart file in notepad++. And I see this below. I don't see the custom property!

<webParts>
  <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
    <metaData>
      <type name="VisualWebPartProject1.VisualWebPart1.VisualWebPart1, VisualWebPartProject1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c139f77c8f6182b2" />
      <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
    </metaData>
    <data>
      <properties>
        <property name="ExportMode" type="exportmode">All</property>
        <property name="HelpUrl" type="string" />
        <property name="Hidden" type="bool">False</property>
        <property name="Description" type="string">My Visual Web Part</property>
        <property name="CatalogIconImageUrl" type="string" />
        <property name="Title" type="string">VisualWebPartProject1 - VisualWebPart1</property>
        <property name="AllowHide" type="bool">True</property>
        <property name="AllowMinimize" type="bool">True</property>
        <property name="AllowZoneChange" type="bool">True</property>
        <property name="TitleUrl" type="string" />
        <property name="ChromeType" type="chrometype">Default</property>
        <property name="AllowConnect" type="bool">True</property>
        <property name="Width" type="unit" />
        <property name="Height" type="unit" />
        <property name="HelpMode" type="helpmode">Navigate</property>
        <property name="AllowEdit" type="bool">True</property>
        <property name="TitleIconImageUrl" type="string" />
        <property name="Direction" type="direction">NotSet</property>
        <property name="AllowClose" type="bool">True</property>
        <property name="ChromeState" type="chromestate">Normal</property>
      </properties>
    </data>
  </webPart>
</webParts>

3 Answers 3

2

Are you sure you created the project as a 2013 project? It looks like you're using the 2010 template for web parts. In 2013, a visual webpart is a class that inherits from webpart. In 2010, there are two classes, one that inherits from web part, and one that inherits from usercontrol. In 2010, there are just a few lines of code in the webpart class that load the usercontrol. So, in 2010, that's where the properties go (in the webpart class, not the usercontrol class.)

So, your code looks fine for a 2013 based web part.

edit: your property code looks fine for a 2013 based web part, but the fact that it inherits from a usercontrol means that it is a 2010 based web part.

Creating properties in 2013 web parts

Creating properies in 2010 web parts

1

You forgot the name (WebDisplayName):

[WebBrowsable(true),
 WebDisplayName("Text Input"), //<----------------Needs a name
 WebDescription("This Accepts text Input"),
 Personalizable(PersonalizationScope.Shared),
 Category("My Category")]
 public string TextProperty1 { get; set; }

Source

1

Try to add WebDisplayName("your field name"), and make sure that the final code looks like this

[WebBrowsable(true),
WebDisplayName("Project ID"),
WebDescription("Enter Project Number"),
Personalizable(PersonalizationScope.Shared),
Category("Project Settings")]
public string ProjectID { get; set; }

Also, you can check the detailed steps in Create custom web part properties for visual web part in SharePoint 2010-2013 article

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.