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 have a webpartpage which in this page I added multiple custom webparts which are the same. I wanted to use the .webpart file to add my own custom property like : value

so I can treat each webpart according to this value.

my question is: how can I access my custom property in the webpart ? I can't seems to get hold of this property (I can access title , description etc. but not my property)

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

TIA Ronen

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

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

share|improve this answer
    
thank you very much both now it works ! –  Ronen Apr 4 '13 at 10:15
    
no probs happy to help ;) –  ali Sharepoint Apr 4 '13 at 10:44
add comment

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>
share|improve this answer
    
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 Apr 4 '13 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. –  Alex Choroshin Apr 6 '13 at 16:32
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.