This SharePoint Rest API tutorial explains, how to create a column in a list using Rest API in SharePoint Online or SharePoint 2013/2016.
Create column in list using Rest API in SharePoint Online/2013/2016
Here in this demo let us take one textbox and one button in HTML form. The user can give a column name in the textbox and click on the Submit button. Once the column created successfully it will display a successful message in the div whose id is “divResults“.
For this particular example, let us use a script editor web part inside a web part page in SharePoint. In that script editor page, you can add the HTML code as well as the rest API code.
HTML Code:
Below is the HTML code
<div>
<strong>Enter Column Name:</strong>
<input type="text" id="txtColumnName" />
<input type="button" id="btnSubmit" value="Create Column" />
</div>
<div id="divResults"></div>
Rest API Code:
Below is the rest api code to create a column in a SharePoint Online list.
Here is data, we are passing the metadata for the column like, Title for the column and also we are providing the column type by using the FieldTypeKind property. This property gets or sets a value that specifies the type of the field.
- FieldTypeKind : 1 -> integer value
- FieldTypeKind : 2 -> single line of text
- FieldTypeKind : 3 -> multiple lines of text etc.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(function () {
bindButtonClick();
});
function bindButtonClick() {
$("#btnSubmit").on("click", function () {
createColumn();
});
}
function createColumn() {
var folderName = $("#txtColumnName").val();
var siteUrl = _spPageContextInfo.webAbsoluteUrl;
var fullUrl = siteUrl + "/_api/web/lists/GetByTitle('MyCompanyInfo')/Fields";
$.ajax({
url: fullUrl,
type: "POST",
data: "{ '__metadata': { 'type': 'SP.Field' }, 'Title':'" + folderName + "', 'FieldTypeKind': 3 }",
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: onQuerySucceeded,
error: onQueryFailed
});
}
function onQuerySucceeded() {
$("#divResults").html("Column Created Successfully !!!");
}
function onQueryFailed() {
alert('Error!');
}
</script>
Now when we Save the page and refresh the page, the HTML will come. Give a name for the column and click on the button, which will successfully create the column in the list.
Now if you can navigate to the list settings page, you can see the new column has been added to the list like below:
You may like following SharePoint Rest API tutorials:
- Display SharePoint List items in a data table using Rest API and jQuery
- Retrieve and Display TASK status using REST API in SharePoint
- Create Highcharts in SharePoint using Rest API
- Update People Picker Group field using Rest API and jQuery in SharePoint 2013/2016
- KnockoutJS SharePoint 2013 CRUD Operations using REST API
- SharePoint 2016 crud operations using Rest API and jQuery on list or document library
- Display SharePoint list item attachments using REST API and jQuery in SharePoint 2013/2016/Online
- How to use Deferred promise and then in Rest API in SharePoint Online or SharePoint 2013?
- Create and delete a file using Rest API in SharePoint Online/2013/2016
This SharePoint tutorial explained, how to create a column in SharePoint list using Rest API in SharePoint Online or SharePoint 2013/2016.
Bhawana Rathore is a Microsoft MVP (3 times in Office Apps & Services) and a passionate SharePoint Consultant, having around 10 years of IT experience in the industry, as well as in .Net technologies. She likes to share her technical expertise in EnjoySharePoint.com and SPGuides.com
[…] Create a column in a list using Rest API in SharePoint Online/2013/2016 […]