I have a custom webpart property optionalText and it is showing in the tool box when we edit the webpart.

Now I want to show that property on webpart itself so that the user can see it without having to edit the webpart. Is there any way to do it?

 /// <summary>
 /// Gets or sets a value of Optional Text.
 /// </summary>
 public string OptionalText
  • You can assign the string value to a label. You won't be able to edit it without editing going to the WebPart Properties. – Vince2322589 Nov 21 '14 at 10:21

You can use below code

var page = "pageurl";
var limitedManager = SPContext.Current.Web.GetLimitedWebPartManager(page, PersonalizationScope.Shared);
//Replace WebPartClass with your webpart class name
var webPart = limitedManager.WebParts.OfType<WebPartClass>().FirstOrDefault();
var text = webPart.OptionalText;

Not each user can personalize the web part properties. So you need to read either user specific details or shared one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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