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 added a check Check Box to a custom list library form which works well with new files uploaded and I can check the property Value by

bool.Parse(listItem.Properties["columnName"].ToString());

but how can I check Property exists or not(for list items that are already there before I added this checkbox column) as otherwise I will get a exception.

share|improve this question

3 Answers 3

up vote 3 down vote accepted

Check if listItem.Properties["columnName"] is null

And you don't have to go through a string if not just use (bool)listItem.Properties["columnName"]

So a full test could be:

if (listItem.Properties["columnName"] != null
 && (bool)listItem.Properties["columnName"])
{
    // Checked
}
else
{
    // Not Checked
}
share|improve this answer

This has worked for me in the past:

if(listItem.Properties["columnName"] != null)
{
 bool.Parse(listItem.Properties["columnName"].ToString());
}
share|improve this answer

If you already have an instance of either the parent List of ListItem you could easily check whether the particular field does exist (e.g. if you use multiple content types you could try directly on the content type), by using one method of the Fields property of type SPFieldCollection - see here for more details http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfieldcollection.containsfield

Then you could check its value for null, etc.

Recomandation: Try using always the InternalName as DisplayName could be changed at any time (both Content Type fields or regular Columns added directly to the list).

share|improve this answer

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.