1

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.

3 Answers 3

3

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
}
1

This has worked for me in the past:

if(listItem.Properties["columnName"] != null)
{
 bool.Parse(listItem.Properties["columnName"].ToString());
}
1

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).

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.