1

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?

2
  • aren't there a solution to put a default value? And what appen if i modify the value (directly in the property in edit webpart from sharepoint). The users has to keep the value they define in the property Thanks
    – bender
    Commented Feb 15, 2014 at 15:04
  • Your current approach is fine, your only problem is that you call .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.
    – eirikb
    Commented Feb 15, 2014 at 18:06

3 Answers 3

0

Do not store titleWP property as static (I think this is the reason why IISReset clears the value)

I would use something like this:

private const string DefaultTitleWP = "Résultats de recherche";
private string _titleWP = DefaultTitleWP;
[Category("Extended Settings"),
Personalizable(PersonalizationScope.Shared),
WebBrowsable(true),
DefaultValue(DefaultTitleWP)
WebDisplayName("Titres de la WebPart"),
WebDescription("Entrez le titre de la WebPart")]
public string TitleWP
{
    get { return _titleWP; }
    set { _titleWP = value; }
}
1

I don't think DefaultValue works very well.

Try this:

titleWP = titleWP ?? "Résultats de recherche";
if (string.IsNullOrEmpty(titleWP.Trim()))
{
    titleWP = "search result";
 }
1
  • I'm not sure what you want though, here if titleWP is null it will be Résultats de recherche, if titleWP is empty it will be search result. This seems strange to me, but it is according with your original code.
    – eirikb
    Commented Feb 15, 2014 at 14:59
1

Why not use

string.IsNullOrWhiteSpace(titleWP)

This will check null, empty or just white space (like row breaks and blanks)

2
  • 1
    Hoping for SharePoint 2013? :)
    – eirikb
    Commented Feb 15, 2014 at 18:00
  • Always have hope;) Commented Feb 15, 2014 at 18:12

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.