Expand Minimize

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.

CheckId SPC050237
TypeName DoNotUseFieldCollectionByIndex
Severity CriticalWarning
Type Assembly

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");

To suppress this violation in managed code add the following attribute to the method which contains the instruction (available since SPCAF version v5.2). Learn more about SuppressMessage here.

// Important: Ensure to have #define CODE_ANALYSIS at the beginning of your .cs file
[SuppressMessage("SPCAF.Rules.BestPracticesGroup", "SPC050237:DoNotUseFieldCollectionByIndex", Justification = "Provide reason for suppression here")]
Disclaimer: The views and opinions expressed in this documentation and in SPCAF do not necessarily reflect the opinions and recommendations of Microsoft or any member of Microsoft. SPCAF and RENCORE are registered trademarks of Rencore. All other trademarks, service marks, collective marks, copyrights, registered names, and marks used or cited by this documentation are the property of their respective owners.