7

I got one custom boolean column/field which I want to check if it exists or not before checking its value, currently I am using this code but it ain't working like it should,

    using (SPWeb web = properties.OpenWeb())
    {
        SPList list = web.Lists["List Name"];

        foreach (SPListItem listItem in list.Items)
        {
            if (listItem.Properties["Boolean Property"] != null &&
                    bool.Parse(listItem.Properties["Boolean Property"].ToString()) == false)
            {
                //continue
            }
            else
            {
                i++;
            }
        }
    }

I am using .Net framework 3.5 so can't use fieldexsist property.

1 Answer 1

12

listItem.Properties will show metadata properties, not fields. Not sure which one you want.

I use the following to check if a field exists:

list.Fields.ContainsField("fieldname")

Likewise, for metadata properties you can use:

listItem.Properties.ContainsKey("property name")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.