Share via


SharePoint Online Office 365 Create column in list using Rest API

Introduction:

Here we will discuss how we can create column in a list using Rest API in SharePoint Online Office 365 site. The same code will also work fine for SharePoint 2013 and SharePoint 2016.

Here in this demo let us take one textbox and one button. A user can give a column name in the textbox and click on Submit button. Once the column created successfully it will display 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 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.

You can see more on this msdn link.

<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.

https://www.enjoysharepoint.com/wp-content/uploads/2018/09/SharePoint-2013-create-column-rest-api.png

Now if you can navigate to the list settings page, you can see the new column has been added to the list like below:

https://www.enjoysharepoint.com/wp-content/uploads/2018/09/Add_column_to_list_using_rest_api_sharepoint_online.png

References:

You can check out few more Rest API articles:

Conclusion:

Here we have discussed how we can create columns to SharePoint lists using Rest API. The same code will work for SharePoint Online, SharePoint 2016/2013.