Latest Posts

Create List Column in SharePoint using REST API

The example in this topic show how to use REST API to Create List Column in SharePoint
  • REST-API is based on OData (Open Data) Protocol and hence it doesn't require any dll or JS library to run the commands on the SharePoint objects.
  • REST-API calls require proper end-point URL and proper Header implementation in your REST call.

How to use SharePoint REST API?

  • Note: This is just a sample, you can go ahead with your implementation.
  • You can also use this method in your SPFx Component.
  • Open your Site. Create a webpart page.
  • Put a Content Editor Web part and a Script Editor Web part on it.
  • Webpart page content editor and scipt editor
  • I have added below Html in Content Editor webpart. You can replace MainFunction with your actual method name.

    <button onclick="MainFunction();" type="button">Click me​</button>

  • And below references in Script Editor Webpart. ScriptFile.js is a JavaScript file where you can keep below function i.e. your actual logic.

    <script src="https://MyServer/sites/SiteCollection/style library/js/ScriptFile.js"></script>

  • Once you are done with setting your page, click on the button and your method should get executed.

  • function CreateListColumn() {
  •     $.ajax
  •         ({
  •             // _spPageContextInfo.webAbsoluteUrl - will give absolute URL of the site where you are running the code.
  •             // You can replace this with other site URL where you want to apply the function
  •  
  •             url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('List Name')/fields",
  •             type: "POST",
  •  
  •             // 'FieldTypeKind' value in below line decide the datatype of the column.
  •             // Some 'FieldTypeKind' values are listed below, after the method, for reference.
  •             data: "{'__metadata':{'type': 'SP.Field'},'FieldTypeKind': 4,'Title':'NewColumnDateTime'}",
  •             headers:
  •                {
  •                    // Accept header: Specifies the format for response data from the server.
  •                    "Accept": "application/json;odata=verbose",
  •                    //Content-Type header: Specifies the format of the data that the client is sending to the server
  •                    "Content-Type": "application/json;odata=verbose",
  •                    // X-RequestDigest header: When you send a POST request, it must include the form digest value in X-RequestDigest header
  •                    "X-RequestDigest": $("#__REQUESTDIGEST").val()
  •                },
  •             success: function (data, status, xhr) {
  •                 console.log("Success");
  •             },
  •             error: function (xhr, status, error) {
  •                 console.log("Failed");
  •             }
  •         });
  • }
  •  
  • /* 'FieldTypeKind' values and their description mentioned below. You can use these values in above method to create column using REST API
  •  
  •  
  • Invalid - Must not be used. The value = 0.
  • Integer - Specifies that the field contains an integer value. The value = 1.
  • Text - Specifies that the field contains a single line of text. The value = 2.
  • Note - Specifies that the field contains multiple lines of text. The value = 3.
  • DateTime - Specifies that the field contains a date and time value or a date-only value. The value = 4.
  • Counter - Specifies that the field contains a monotonically increasing integer. The value = 5.
  • Choice - Specifies that the field contains a single value from a set of specified values. The value = 6.
  • Lookup - Specifies that the field is a lookup field. The value = 7.
  • Boolean - Specifies that the field contains a Boolean value. The value = 8.
  • Number - Specifies that the field contains a floating-point number value. The value = 9.
  • Currency - Specifies that the field contains a currency value. The value = 10.
  • URL - Specifies that the field contains a URI and an optional description of the URI. The value = 11.
  • Computed - Specifies that the field is a computed field. The value = 12.
  • Threading - Specifies that the field indicates the thread for a discussion item in a threaded view of a discussion board. The value = 13.
  • Guid - Specifies that the field contains a GUID value. The value = 14.
  • MultiChoice - Specifies that the field contains one or more values from a set of specified values. The value = 15.
  • GridChoice - Specifies that the field contains rating scale values for a survey list. The value = 16.
  • Calculated - Specifies that the field is a calculated field. The value = 17.
  • File - Specifies that the field contains the leaf name of a document as a value. The value = 18.
  • Attachments - Specifies that the field indicates whether the list item has attachments. The value = 19.
  • User - Specifies that the field contains one or more users and groups as values. The value = 20.
  • Recurrence - Specifies that the field indicates whether a meeting in a calendar list recurs. The value = 21.
  • CrossProjectLink - Specifies that the field contains a link between projects in a Meeting Workspace site. The value = 22.
  • ModStat - Specifies that the field indicates moderation status. The value = 23.
  • Error - Specifies that the type of the field was set to an invalid value. The value = 24.
  • ContentTypeId - Specifies that the field contains a content type identifier as a value. The value = 25.
  • PageSeparator - Specifies that the field separates questions in a survey list onto multiple pages. The value = 26.
  • ThreadIndex - Specifies that the field indicates the position of a discussion item in a threaded view of a discussion board. The value = 27.
  • WorkflowStatus - Specifies that the field indicates the status of a workflow instance on a list item. The value = 28.
  • AllDayEvent - Specifies that the field indicates whether a meeting in a calendar list is an all-day event. The value = 29.
  • WorkflowEventType - Specifies that the field contains the most recent event in a workflow instance. The value = 30.
  • MaxItems - Must not be used. The value = 31.
  •  
  • */

Thank you for reading this article. This code was tested in SharePoint 2013

We value your Feedback:

Page URL:

Name:

Email:


Suggestion:

© 2024 Code SharePoint