Wednesday, 23 May 2012

Save User Inputs into the List using Client Side Object Model (Ecma Script)


      
 Save User Inputs into the List using Client Side Object Model (Ecma Script)
 

<script type="text/javascript">

     function callscript() {

        var currentuser = null;

        ExecuteOrDelayUntilScriptLoaded(addlist, "sp.js");

    }

    function addlist() {

//To get the User Inputs from Form..

        var Name = document.getElementById("Name");

        var mail = document.getElementById("Mail");

        var company = document.getElementById("Company");

        saveitem(Name.value, mail.value, company.value); 

    }

    function saveitem(cname, cmail, ccompany) { 

        var spcontext = null;

        var spweb = null;

        var splist = null;


      //To get Current context..

        spcontext = new SP.ClientContext.get_current();

        spweb = spcontext.get_web();


     //To get “Contact” list

        splist = spweb.get_lists().getByTitle("Contact");

        var listcreation = null;

        listcreation = new SP.ListItemCreationInformation();

        var items = null;


       //To add list creation information to list.

        items = splist.addItem(listcreation);


      //To set items to the List “Contact”

        items.set_item('Name', cname);

        items.set_item('Mail', cmail);

        items.set_item('Company', ccompany);

        items.update();

        spcontext.load(splist);

        spcontext.executeQueryAsync(success, fail);

    }

    function success() {

        alert('Data saved Successfully');

    }

    function fail(sender, args) {

        alert('error' + args.get_Message() + '\n' + args.get_stackTrace());

    }       

</script>


      <asp:Label ID="lblname" Text="Name" runat="server"></asp:Label> &nbsp;&nbsp
      <input type="text" id="Name" /><br /><br />   

       <asp:Label ID="lblmail" Text="Mail Id" runat="server"></asp:Label>& nbsp;&nbsp  
       <input type="text" id="Mail" /><br /><br />

        <asp:Label ID="lblcompany" Text="Company" runat="server"></asp:Label>& nbsp;&nbsp 
        <input type="text" id="Company" /><br /><br /> &nbsp;&nbsp;&nbsp;&nbsp;

      <input type="button" id="submit" value="Save" onclick="javascript:callscript();" />    


     ......................................................................................................................................................












1 comment:

  1. Hi,

    I am getting below error messages in IE8

    1)object expected
    2)object doesn't support this property or method

    ReplyDelete