Insert data to SharePoint List using JSOM

Insert data to SharePoint List using JSOM
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
 <script type="text/javascript">
$(document).ready(function () {     
        $("#btnSubmit").click(function () {
            createItemsToList();
        });
    });


    function createItemsToList() {     
         //List Name
        var listTitle = "EmployeeList";

        //Get the current client context   
        context = SP.ClientContext.get_current();
        var List = context.get_web().get_lists().getByTitle(listTitle);

        //Create a new record
        var listItemCreationInformation = new SP.ListItemCreationInformation();
        var listItem = List.addItem(listItemCreationInformation);

        //Set the values
        listItem.set_item('Title', txtEmpName.val());
        listItem.set_item('Email', txtEmail.val());
        listItem.set_item('City', txtCity.val());
        listItem.set_item('Designation', txtDesignation.val());
        listItem.set_item('Age', txtAge.val());
     

        listItem.update();
        context.load(listItem);

        context.executeQueryAsync(createItemsToListSucceeded, createItemsToListFailed);

    }

    function createItemsToListSucceeded() {
        alert('List Item Added.');
    }

    function createItemsToListFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
</script>

Comments

Popular posts from this blog

How to Customize exported excel's cell background color in Datatables

Populate dropdown using jquery ajax in SharePoint

Customizing SharePoint list and implementing Quick search using jQuery