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 May 19 '16 at 8:44
  • Tried given Solutions in comments? – Hardik May 19 '16 at 12:17
  • @swathi patil, Hardik solution got worked for you? – Mihir Dec 8 '17 at 13:30
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 – swati patil May 19 '16 at 8:50
  • Please check the answers here...social.technet.microsoft.com/Forums/en-US/… – Hardik May 19 '16 at 8:53
  • I think you should try JSOM.... codeproject.com/Articles/561309/…............... dotnetcurry.com/sharepoint/1028/… – Hardik May 19 '16 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. – swati patil May 19 '16 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 May 19 '16 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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