By using below code we can create fields of different types using ECMAScript:
function createFields()
{
currentcontext = new SP.ClientContext.get_current();
web = clientContext.get_web();
this.list = web.get_lists().getByTitle(‘ListName’);
//Add new fields to the list
//Single line of text Field
this.newColumn = oList.get_fields().addFieldAsXml("", true,
SP.AddFieldOptions.defaultValue);
//Multi line of text Field
this.newColumn = oList.get_fields().addFieldAsXml("", true,
SP.AddFieldOptions.defaultValue);
//Boolean Field
this.newColumn = oList.get_fields().addFieldAsXml("", true,
SP.AddFieldOptions.defaultValue);
//Number Field
this.newColumn = oList.get_fields().addFieldAsXml("", true,
SP.AddFieldOptions.defaultValue);
//DateTime Field
this.newColumn = oList.get_fields().addFieldAsXml("", true,
SP.AddFieldOptions.defaultValue);
//People Picker Field
this.newColumn = oList.get_fields().addFieldAsXml("", true,
SP.AddFieldOptions.defaultValue);
//Choice field(dropdown)
this.newColumn= oList.get_fields().addFieldAsXml("ChoiceAChoiceAChoiceBChoiceCChoiceD", true,
SP.AddFieldOptions.defaultValue);
currentcontext.load(this.newfield);
currentcontext.executeQueryAsync(Function.createDelegate(this, this.onListCreateSuccess), Function.createDelegate(this, this.onQueryFailed));
}
function onListCreateSuccess(sender, args) {alert('sucess');
alert("Field Name: " + this.newfield.get_title());
}
function onQueryFailed(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}
Note: SP.Field.typeAsString Property is used to Gets or sets a value that specifies the type of the field.[From Msdn:
http://msdn.microsoft.com/en-us/library/ee553500.aspx]