Wednesday, 25 January 2012

Creating a List Item Using ECMAScript (JavaScript, JScript)


<table width="364" height="83" border="1">
  <tr>
    <td>Enter the Title:</td>
    <td><input type="text" id="titletxt" name="titletxt"> </td>
  </tr>
  <tr>
    <td>Enter the Name:</td>
    <td><input type="text" id="nametxt" name="nametxt"> </td>
  </tr>
  <tr>
    <td colspan="2"><input id="btnGetFieldType" onclick="getFieldType()" type="button" value="Get Field Type" align="middle" /></td>
   
  </tr>
</table>
<script language="ecmascript" type="text/ecmascript">
        var fieldCollection;
        var field;
        var list;
var t=document.getElementById('titletxt');
var n=document.getElementById('nametxt');
        function getFieldType() {
            var clientContext = SP.ClientContext.get_current();
            if (clientContext != undefined && clientContext != null) {
                var oList = clientContext.get_web().get_lists().getByTitle('ecmascript List');
       
    var itemCreateInfo = new SP.ListItemCreationInformation();
    this.oListItem = oList.addItem(itemCreateInfo);
       
    oListItem.set_item('Title',t.value);
    oListItem.set_item('Name',n.value);
       
    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>

2 comments: