0

According to MSDN, we can create field like below..

$.ajax({
  url: "http://<site url>/_api/web/lists(guid'da58632f-faf0-4a78-8219-99c307747741')
    /fields",
  type: "POST",
  data: "{ '__metadata': { 'type': 'SP.Field' }, 'Title': 'Comments', 'FieldTypeKind': 3 }",
  headers: { 
    "X-RequestDigest": <form digest value>,
    "accept": "application/json; odata=verbose",
    "content-type": "application/json;odata=verbose",
    "content-length": <length of body data>
  },
  success: successHandler,
  error: errorHandler
});

But question is how to create multiple fields with their datatypes. Like I want to create fields EmpName as Single line of text, Department as lookup, Profile as Hyperlink. How I can create metadata for all these fields?

3
  • Have you tried below given solution? is that works for you?
    – Hardik
    Commented May 19, 2016 at 8:44
  • Tried given Solutions in comments?
    – Hardik
    Commented May 19, 2016 at 12:17
  • @swathi patil, Hardik solution got worked for you?
    – Mihir
    Commented Dec 8, 2017 at 13:30

1 Answer 1

0

Please try below mentioned code if it works for you.

var FirstRow = { '__metadata': { 'type': 'SP.Field' }, 'Title': 'Comments', 'FieldTypeKind': 3 }
var SecondRow = { '__metadata': { 'type': 'SP.Field' }, 'Contact': 'Comments', 'FieldTypeKind': 3 }

$.ajax({
  url: "http://<site url>/_api/web/lists(guid'da58632f-faf0-4a78-8219-99c307747741')
    /fields",
  type: "POST",
  data: JSON.stringify({ FRow: FirstRow, SRow: SecondRow }),
  headers: { 
    "X-RequestDigest": <form digest value>,
    "accept": "application/json; odata=verbose",
    "content-type": "application/json;odata=verbose",
    "content-length": <length of body data>
  },
  success: successHandler,
  error: errorHandler
});

Please change the data in above code as per your environment (need).

7
  • Hi Hardik.... Thanks for the reply .... I have tried your above code but I got forbidden error Commented May 19, 2016 at 8:50
  • Please check the answers here...social.technet.microsoft.com/Forums/en-US/…
    – Hardik
    Commented May 19, 2016 at 8:53
  • I think you should try JSOM.... codeproject.com/Articles/561309/…............... dotnetcurry.com/sharepoint/1028/…
    – Hardik
    Commented May 19, 2016 at 8:54
  • Thanks Hardik for your quick reply but I have done this task by using JSOM and its working fine but we want to do complete this task by using REST and also I have gone through the REST code which is available in first link but that code is for to add one field at a time but when we want to add multiple fields with their data type then we need to call again and again that creating function ,that function is not useful for add the bunch of fields in list in single request and we want to do it only in single request. Commented May 19, 2016 at 9:42
  • You are getting 403 Forbidden error. So have you used these two lines? ,........$wc.UseDefaultCredentials = $true $wc.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")
    – Hardik
    Commented May 19, 2016 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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