SharePoint Online/2013: Create Site Column using JavaScript Object Model and Rest API

This SharePoint tutorial explains, how to create a site column using the JavaScript client object model as well as how to create a site column using Rest API in SharePoint 2013/2016 or SharePoint Online.

Site columns in SharePoint 2013/2016/Online are reusable fields which we can use across multiple, navigate to the Site Settings page lists within a site. So if you have created a site column in a site, then the site column will be available to use in the current site as well as the child sites in the site hierarchy. So if you have created a site column in a top-level site in the site collection, then the site column will be available to use in the entire site collection.

Out of box, SharePoint has more than 400 site columns in the top level site, out of them many are also hidden. You can navigate to Site Column gallery from the site settings page to see the site columns.

Create site column using JavaScript Object Model

We can use the JavaScript client object model to create a site column in SharePoint. You can create any web part page and then add a script editor web part on it. Then put the below code and Save the page.

<input type='button' value='Create Site Column' onclick="createSiteColumn();"/>

<script language="javascript" type="text/javascript">

function createSiteColumn() {
var xmlDef = "<Field DisplayName='WebSiteName' Type='Text'/>";
var ctx = new SP.ClientContext.get_current();
var field = ctx.get_web().get_fields().addFieldAsXml(xmlDef, false, SP.AddFieldOptions.addToNoContentType);
ctx.load(field);
field.set_group("EnjoySharePoint Site Columns");
field.updateAndPushChanges(false);
ctx.executeQueryAsync(success, failure)
}

var success = function () {
alert('Site column created successfully !!!');
}

var failure = function () {
alert('Error occured while creating site column.');
}
</script>

Click on the button, it will display a message saying “Site column created successfully !!!” if the site column created successfully.

Now navigate to the Site Settings page, then click on “Site columns ” under Web Designer Galleries . In the Site Columns page, search based on EnjoySharePoint Site Columns group, you will be able to see our site column name as: WebSiteName like below:

sharepoint create site column javascript
sharepoint create site column javascript

Create site column using REST API in SharePoint 2013/2016/Online

Below is the code to create a site column in SharePoint 2013 using Rest API. You can put the below code in a script editor web part or content editor web part in SharePoint 2013/2016 or SharePoint Online.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

<script>
$(function(){
$("#btnClick").click(function(){
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/fields";

$.ajax({
url: requestUri,
type: "POST",
data: JSON.stringify(
{ '__metadata': { 'type': 'SP.Field' },
'Title': 'Web Site Lists',
'FieldTypeKind': 2,
'Group': 'EnjoySharePoint Site Column'
}),
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: onSuccess,
error: onError

});

function onSuccess(data) {
alert('Site column created successfully');
}

function onError(error) {
alert(JSON.stringify(error));
}
});

});
</script>

<input type="button" id="btnClick" value="Create Site column using REST API"/>

Here FieldTypeKind determines which type the field type is. 2 means the type is Text. You can check out this article to know the value for different types.

Click on the button, it will display a message saying “Site column created successfully !!!” if the site column created successfully. It will be displayed like below:

sharepoint rest api create site column
sharepoint rest api create site column

You may like following SharePoint site column tutorials:

Hope this SharePoint tutorial explains, how to create a site column in SharePoint using Rest API and JavaScript object model.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SPGuides.com

>