Click here to Skip to main content
13,201,940 members (76,389 online)
Click here to Skip to main content
Add your own
alternative version

Stats

16.2K views
Posted 13 Mar 2013

Add Fields to SharePoint 2013 List Using JSOM (ECMAScript Client Object Model)

, 13 Mar 2013
Rate this:
Please Sign up or sign in to vote.
How to add fields to an existing list

Creating a List

In my previous post, I described how to add a list to SharePoint. In here, I'm going to describe how to add fields to an existing list. First, you need to refer to the same JavaScript files in your script page (SP.core.js, SP.runtime.js, SP.js).

There are a few approaches that you can use to add fields to the existing list. But in my personal opinion, I like the following approach because:

  • It is easy rather that use XML to add entire field
  • Intelligence are support based on field type (text, choice, user...)
  • Code is more readable than XML
<script type="text/ecmascript">
    function addFields() {
          
        var clientContext = new SP.ClientContext.get_current();
        var oWebsite = clientContext.get_web();
        //Getting reference to the list
        oList = clientContext.get_web().get_lists().getByTitle('CustomList');
        
        // Get filed collection
        var fldCollection = oList.get_fields();

        var f1 = clientContext.castTo(
                        fldCollection.addFieldAsXml('<Field Type="Text" 
                        DisplayName="NewField" Name="NewField" />', true, 
                        SP.AddFieldOptions.addToDefaultContentType),
                        SP.FieldText);
        f1.set_title("CurrentDate");
        f1.set_description("sample desc");
        f1.update();       
       
        var f2 = clientContext.castTo(
                    oList.get_fields().addFieldAsXml('<Field Type="Choice" 
                    DisplayName="choice" Name="fldchoice" />', true, 
                    SP.AddFieldOptions.addToDefaultContentType),
                    SP.FieldChoice);
        var choices = Array("Approved", "Rejected", "Progress");
        f2.set_choices(choices);
        f2.update();

        clientContext.executeQueryAsync(
            Function.createDelegate(this, this.onQuerySucceeded),
            Function.createDelegate(this, this.onQueryFailed)
            );
    }

    function onQuerySucceeded() {
        alert("List Field Updated");
    }

    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }

</script>

In this example, I'm adding SharePoint Choice Field (SP.FieldChoice) and SharePoint Text Field to existing SharePoint list.

Note: You can you addFieldAsXml and can give complete XML as a string to add the field as well.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Melick
Sri Lanka Sri Lanka
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171020.1 | Last Updated 13 Mar 2013
Article Copyright 2013 by Melick
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid