Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

SPFieldCollection.Add method (String, SPFieldType, Boolean)

Creates a field in the collection based on the specified display name, field type, and Boolean value.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

public string Add(
	string strDisplayName,
	SPFieldType type,
	bool bRequired


Type: System.String

A string that specifies the display name of the field.

Type: Microsoft.SharePoint.SPFieldType

A SPFieldType value that specifies the type of field to create.

Type: System.Boolean

true if the field is required to contain a value; otherwise, false.

Return value

Type: System.String
A string that contains the internal name of the new field.

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, 

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.

© 2015 Microsoft