SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am currently parsing boolean into string using this line, but it ain't working all of times, is there any alternative ?

bool.Parse(listItem.Properties["Boolean Property"].ToString()) == false
share|improve this question
up vote 2 down vote accepted
bool? temp = listItem.Properties["Boolean Property"] as bool;
bool result = temp ?? true;

This code will read the property as a boolean, though return null if the property is null or not a bool (hence it is put into the nullable bool 'temp'). We then make it a normal bool use the ?? operator - if temp is null, result will be set to 'true'. Otherwise, result is set to whatever value temp has.

Using the '.Tostring()' risks a NullReferenceException if the property isn't set...

share|improve this answer
I was checking if property exists in code above using "ContainsField" but it was throwing exception for converting string into boolean but thanks anyway I give it a try, cheers – Muhammad Raja Oct 9 '12 at 11:34
No problem, that's the pattern I use now, 'cos it avoids the .ToString (I did get caught but NullRef exceptions), and the same pattern works nicely with other types, such as ints. – Andy Burns Oct 9 '12 at 11:35
string valueS = "true";


hope it helps :)

share|improve this answer

Your Answer


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.