0

I am trying to create list fields using REST api. But when I call the code given below many times, it creates some of the fields and not all of them.

code:

function addnewfield(name){
  $.ajax({
        url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/Lists/getbytitle('Flight')/fields",
        type: "POST",
        data: JSON.stringify({ '__metadata': { 'type': 'SP.FieldText' }, 'Title':''+name+'', 'FieldTypeKind': 2,"StaticName": ""+name+"" }),

        headers: {

             "X-RequestDigest": $("#__REQUESTDIGEST").val(),
              "accept": "application/json;odata=verbose",
              "content-type": "application/json;odata=verbose",
              "odata-version": "3.0"
        },
        success: function (data) {
            //console.log("done");
            //console.log(data)
            console.log("added: " + name);
        },
        error: function (response) {
            console.log(response.responseText);
        }
    });

}

Thanks in advance.

  • Which sharepoint version are you using? – Ganesh Sanap Nov 5 '18 at 13:51
  • sharepoint 2013 – Shams Eldeen Nov 5 '18 at 13:53
0

I think the problem is with your data property. Try using below code:

body: "{ '__metadata': { 'type': 'SP.Field' }, 'FieldTypeKind': 2, 'Title':'" + fieldName + "'}"

However you can also use JSOM to create multiple fields in a list.

Sources:

  1. Add a Field in the List in SharePoint 2013 Online Using REST API.

  2. Add Fields to SharePoint 2013 List Using JSOM.

  • i'm trying to add 5 field the code only creates 3 of them and in another time it creates only 2 – Shams Eldeen Nov 5 '18 at 14:03
  • Have you considered using JSOM to create multiple fields at a time? – Ganesh Sanap Nov 5 '18 at 14:33
  • how can i do that? – Shams Eldeen Nov 6 '18 at 8:09
  • Try 2nd link given in above answer. – Ganesh Sanap Nov 6 '18 at 10:22
  • Have you tried this? – Ganesh Sanap Nov 11 '18 at 4:19

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.