Sunday, May 19, 2013

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

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)


*Thanks Ofer Gal for pointing this out.

No comments: