The issue lies with the case of the <field>
and </field>
tag.
It should be <Field>
and </Field>
. Its case sensitive.
Also, to make it required, it needs to TRUE
.
So, try the below code. Works at my end :
var context = SP.ClientContext.get_current()
var fields = context.get_web().get_fields();
var formatted = "<Field Name='ProgramName' DisplayName='Program Name' Required='true'"Required='TRUE'"
+ " Group='Awesome' Type='Text' MaxLength='100'></Field>";
fields.addFieldAsXml(formatted, false, SP.AddFieldOptions.addFieldCheckDisplayName);
context.executeQueryAsync(success, failure);
function success() {
console.log("Request success");
}
function failure(sender, args) {
console.error("Request failed: " + args.get_message());
}
Reference Field element