I'm new to SharePoint and I'm developing a WebPart.
I'm trying to create some properties with a DefaultValue
, but they stay null.
Here is how I'm defining my property in my class:
public static string titleWP;
[Category("Extended Settings"),
Personalizable(PersonalizationScope.Shared),
WebBrowsable(true),
DefaultValue("Résultats de recherche"),
WebDisplayName("Titres de la WebPart"),
WebDescription("Entrez le titre de la WebPart")]
public string _titleWP
{
get { return titleWP; }
set { titleWP = value; }
}
And in CreateChildControls
, if I do this, I get an exception because titleWP
is null
if (string.IsNullOrEmpty(titleWP.Trim()))
{
titleWP = "search result";
}
How can I fix this?
Edit:
Strange thing is if I do an IISRESET or update-spsolution
(because make changes) the value that the users enter disappears and it retrieves the default value.
What is the best practice for defining a property and don't lose what the user modify?
.Trim()
on a variable which can be null, you can solve this by(titleWP ?? "").Trim()
. And this is initially, when users save the variable will have a value. I'm a bit baffled by your unconventional naming (underscore for property, none for private variable), but it should work.