top of page
Writer's pictureSofia Sondh

Create a SharePoint Site Column using Rest API

If you want to make SharePoint work better for your team and customize it according to your needs, using the Rest API is a smart move. In this article, we'll show you how to create SharePoint site columns easily with the Rest API. It's a great way to personalize SharePoint, giving you more control and making it work just the way you want it to.


Read:

Let's explore how the Rest API can help you customize and improve your SharePoint experience.


Create a SharePoint Site Column Using REST API


1. Connect to Your SharePoint Site

Use the appropriate authentication method (e.g., app-only context or user context) to connect to your SharePoint site.


2. Define Column Details

Decide on the column name, internal name, group, and column type (e.g., Single Line of Text, Choice, Date and Time, etc.).


3. Construct the REST API Endpoint

Use the appropriate REST API endpoint for creating a site column. The base URL is typically _api/SPSiteManager.


4. Set the Required Parameters

Specify the necessary parameters in the request body, such as Title, WebTemplate, SiteDesignId, and any other relevant properties.


5. Make a POST Request

Execute a POST request to the REST API endpoint with the required headers and request body.


6. Verify the Column Creation

Check your SharePoint site settings or use additional REST API calls to verify that the site column was successfully created.


Example: Create a SharePoint Site Column Using REST API

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

<script>
$(function() 
{
    // When the button with ID "btnClick" is clicked
    $("#btnClick").click(function() 
	{
        // Construct the REST API endpoint for creating a site column
        var requestUri = _techplatformPageContextInfo.webAbsoluteUrl + "/_api/web/fields";
        // Get the site column name from the input field
        var siteColumnname = $("#txtSiteColumnName").val();
        $.ajax({
            url: requestUri,
            type: "POST",
            data: JSON.stringify({
                '__metadata': {
                    'type': 'TP.Field'
                },
                'Title': siteColumnname,
                'FieldTypeKind': 2, // Specify the field type (2 for Single Line of Text)
                'Group': 'TechPlatform Site Columns' // Specify the group where the column will be created
            }),
            headers: {
                "accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val() // Include the request digest token
            },
            success: onSuccess, // Callback function for successful response
            error: onError // Callback function for error response
        });

        function onSuccess(data) {
            alert('Site column created successfully'); // Show success message
        }

        function onError(error) {
            alert(JSON.stringify(error)); // Show error details
        }
    });
});
</script>

<table>
    <tr>
        <td>Enter Site Column Name: <input type="text" id="txtSiteColumnName"></td>
        <td><input type="button" id="btnClick" value="Create Site column using REST API"></td>
    </tr>
</table>

In this article, we’ve explored how to create SharePoint site columns programmatically using the REST API. By using REST endpoints, you can automate site creation and customize your SharePoint environment to meet specific business needs.

Comments


bottom of page