2

How can I access a custom property in my webpart?

I have a page that contains webparts, which contains multiple instances of the same custom webpart. I wanted to use the .webpart file to add my own custom property like :

<property name="CustomProperty" type="string">value</property>

so I can treat each webpart according to this value.

I can't seems to get hold of this property. I can access title , description etc. but not my property.

I managed to create a custom property which I can handle while editing the webpart at runtime, but I would like to set it once in the .webpart file.

2 Answers 2

1

as alex stated,

Also you need to make sure your actualy putting in the values first ;), You need a constant with a value or "" , then the getter setter with that const varible to get and set, you finaly need to add a constructor that contains the private varibles that pass the const to the private:

create your base class:

add const:

//this is your default string
const string c_MyStringDefault = "Sample String";

add private string:

//this will be used internaly through the constructor
private string _myString;

add constructor:

   // Constructor
   public  CustomPropertyWebPart()
   {
    // Initialize private variables.
    _myString = c_MyStringDefault;
   }

add the get/set method

    // Creates a custom property that is a string.
    // This property will be displayed as a text box in the
    // property pane.

    // Create a custom category in the property sheet.
    [Category("Custom Properties")]
    // Assign the default value.
    [DefaultValue(c_MyStringDefault)]
    // Property is available in both Personalization
    // and Customization mode.
    [WebPartStorage(Storage.Personal)]
    // The caption that appears in the property sheet.
    [FriendlyNameAttribute("Custom String")]
    // The tool tip that appears when pausing the mouse pointer over
    // the friendly name in the property pane.
    [Description("Type a string value.")]
    // Display the property in the property pane.
    [Browsable(true)]
    [XmlElement(ElementName="MyString")]
    // The accessor for this property.
    public string MyString
    {
        get
        {
            return _myString;
        }
        set
        {
            _myString = value;
        }
    }

http://msdn.microsoft.com/en-gb/library/dd584174(v=office.11).aspx

1
  • no probs happy to help ;)
    – Ali Jafer
    Commented Apr 4, 2013 at 10:44
1

In your custom web part create a public property:

[Browsable(false)]
public string Value { get; set; }

Then you can access the public property and set it in your CustomWebpart.webpart file.

<?xml version="1.0" encoding="utf-8"?>
<webParts>
  <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
    <metaData>
      <type name="CustomWebpart, $SharePoint.Project.AssemblyFullName$" />
      <importErrorMessage>$Resources:core,ImportErrorMessage;</importErrorMessage>
    </metaData>
    <data>
      <properties>
        <property name="Title" type="string">myWebpart</property>
        <property name="Description" type="string">My Visual WebPart</property>
        <property name="Value" type="string">your value</property>
      </properties>
    </data>
  </webPart>
</webParts>
2
  • Thank you very much, I did as suggested but for some reason I get null for this property even that I set value in it. any suggestions ?
    – Ronen
    Commented Apr 4, 2013 at 8:24
  • Hi Ronen, try adding the [Browsable(false)] attribute to your property, I think it will solve your problem. See an updated code example above. Commented Apr 6, 2013 at 16:32

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.