Wednesday, 4 December 2013

Insert,update,delete in sharepoint list using Javascript

Share it Please

<input type="button" value="Submit"  onclick="createListItem();" style="width: 89px" />
<input type="button" value="Update"  onclick="updateListItem();" style="width: 89px" />
<input type="button" value="Delete"  onclick="deleteListItem();" style="width: 89px" />

<script type="text/javascript">
    var siteUrl = '/sites/TeamSite1';
    function createListItem() {
        var clientContext = new SP.ClientContext(siteUrl);
        var oList = clientContext.get_web().get_lists().getByTitle('Employee');
        var itemCreateInfo = new SP.ListItemCreationInformation();
        this.oListItem = oList.addItem(itemCreateInfo);
        oListItem.set_item('Title', '1');
        oListItem.set_item('EmpName', 'Mohamed sithik');
        oListItem.set_item('Age', '27');
        oListItem.set_item('Address', 'Chennai');
        oListItem.update();
        clientContext.load(oListItem);
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    }
    function onQuerySucceeded() {
        alert('Item created: ' + oListItem.get_id());
    }
    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }

</script>
<script>
    var siteUrl = '/sites/TeamSite1';
    function updateListItem() {
        var clientContext = new SP.ClientContext(siteUrl);
        var oList = clientContext.get_web().get_lists().getByTitle('Employee');
        this.oListItem = oList.getItemById(333);
        oListItem.set_item('Title', '4');
        oListItem.set_item('EmpName', 'sithik');
        oListItem.set_item('Age', '26');
        oListItem.set_item('Address', 'Bangalore');
        oListItem.update();
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    }
    function onQuerySucceeded() {
        alert('Item updated!');
    }
    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
</script>
<script>
     var siteUrl = '/sites/TeamSite1';

     function deleteListItem() {
         this.itemId = 333;
         var clientContext = new SP.ClientContext(siteUrl);
         var oList = clientContext.get_web().get_lists().getByTitle('Employee');
         this.oListItem = oList.getItemById(itemId);
         oListItem.deleteObject();
         clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
     }

     function onQuerySucceeded() {
         alert('Item deleted: ' + itemId);
     }

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


    </script>

No comments:

Post a Comment

Followers

Follow The Author