SPC050237: Do not request SPField from SPFieldCollection by index. Use SPFieldCollection.GetField() instead.

Calling SPFieldCollection[] to retrieve a SPField in a web could fail if not the display name is used as argument. Better use SPWeb.Fields.GetField() which returns the field with the specified internal, display, or static name.

TypeName: DoNotUseFieldCollectionByIndex
CheckId: SPC050237
Severity: CriticalWarning
Type: AssemblyFileReference
Resolution

Replace calls to SPFieldCollection[] with SPFieldCollection.GetField(). SharePoint searches first by internal name; then, if necessary, by display name; finally, if necessary, by static name.

Bad Practice:

SPWeb myWeb = SPContext.Current.Web;
SPField newField = web.Fields["FieldName"]; // could fail if FieldName is not the display name
Good Practice:
SPWeb myWeb = SPContext.Current.Web;
SPField newField = web.Fields.GetField("FieldName");

Links

comments powered by Disqus