SPFieldCollection.Add method (String, SPFieldType, Boolean)
SharePoint 2013
Creates a field in the collection based on the specified display name, field type, and Boolean value.
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Parameters
- strDisplayName
- Type: System.String
A string that specifies the display name of the field.
- type
- Type: Microsoft.SharePoint.SPFieldType
A SPFieldType value that specifies the type of field to create.
- bRequired
- Type: System.Boolean
true if the field is required to contain a value; otherwise, false.
The Add method cannot be used to create a field of type Lookup, because this causes an SPException to be thrown. Instead use the AddLookup method to add a field of this type.
The following code example adds a required text field to the specified list.
using (SPWeb oWebsite = SPContext.Current.Site.AllWebs["MySite"]) { SPFieldCollection collFields = oWebsite.Lists["MyList"].Fields; collFields.Add("MyField", Microsoft.SharePoint.SPFieldType.Text, true); }
Note
|
|---|
Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objects. |
Note