Finding if a site column exists in a site, by ID

document column

Scenario - you have an ID of a site column (SPField belonging to SPWeb)and you want to find out if there is a field by that ID in the collection.

Problem: if you try something like:

web.Fields[fieldID] == null

The result is an exception if the field doesnt exist. What a shame.

The solution is to use the Contains method of the Fields collection:

web.Fields.Contains(fieldID)

Certifications

microsoft sharepoint server 2010 certified master
microsoft sharepoint 2016 certified professional badge
microsoft certified expert badge

Subscribe for Sharepoint Tips & Tricks

Exclusive offers in your inbox

© Copyright 2023 Sharepoint Tips