Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Call function on button click for adding items to sharepoint list.

$
0
0

On button click how can I call the addlistitem() function.I am looking to add items on button click...New to csom,dont have much idea how to achieve this.

<html>
<script src=" " type="text/javascript"></script>
<script type="text/javascript">
 $(document).ready(function()
 {
  ExecuteOrDelayUntilScriptLoaded(AddListItem, "sp.js");
 });

function AddListItem(){
    var ListName = "MyList";
    var context = new SP.ClientContext.get_current(); // the current context is taken by default here
    //you can also create a particular site context as follows
    //var context = new SP.ClientContext('/Sites/site1');
    var lstObject = context.get_web().get_lists().getByTitle(ListName);
    var listItemCreationInfo = new SP.ListItemCreationInformation();
    var newItem = lstObject.addItem(listItemCreationInfo);
    newItem.set_item('Title', 'This is new item');
    // set values to other columns of the list here
    newItem.update();
    context.executeQueryAsync(Function.createDelegate(this, this.onSuccess),
        Function.createDelegate(this, this.onFailure));
 
     function onSuccess() {
 
        alert('Item created: ' + newItem.get_id());
    }
 
    function onFailure(sender, args) {
 
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
}

</script>
<body>
    <input type="button" value="submit" id="btnSub" />
    </html>
</body>   


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>