ECMA Script : To create a new list item in SharePoint 2010

 

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

 

    var clientContext = null;

    var oWeb = null;

    var oListColl = null;

    var oList = null;

    var oListItem = null;

    var listItemCreationInfo = null;

  

 

    function createListItem() {

        clientContext = new SP.ClientContext.get_current();

        oWeb = clientContext.get_web();

        oListColl = oWeb.get_lists();

        oList = oListColl.getByTitle('Custom List');

        listItemCreationInfo = new SP.ListItemCreationInformation();

        oListItem = oList.addItem(listItemCreationInfo);

        oListItem.set_item('Title', 'New Item');

        oListItem.update();

        clientContext.load(oListItem);

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

    }

 

    function onQuerySucceeded() {

       

        alert(oListItem.get_title() + ' item is created successfully.');

    }

 

    function onQueryFailed(sender, args) {

        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());

    }

 

 

 

</script>

<input id="btnCreateListItem" onclick="createListItem()" type="button" value="Create List Item"/>

Ebook Download
View all
Learn
View all