Skip to main content
added 51 characters in body
Source Link
Gautam Sheth
  • 31k
  • 1
  • 36
  • 62

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

The issue lies with the case of the <field> and </field> tag.

It should be <Field> and </Field>. Its case sensitive

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'"
   + " 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

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'"
   + " 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

Source Link
Gautam Sheth
  • 31k
  • 1
  • 36
  • 62

The issue lies with the case of the <field> and </field> tag.

It should be <Field> and </Field>. Its case sensitive

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'"
   + " 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