Creating a list Item using Javascript and webservices in SharePoint

I came across the requirement from customer to avoid postbacks while creating list item for WSS 3.0 application.

Below code is executing completly at client side by using webservice of List.asmx in Javascript,

<h2>Read File in Javascript</h2>
<script language=”JavaScript”>
var list;
var filePath;
function CreateListItem(listname)
  var url = “http://siteurl/_vti_bin/Lists.asmx“;
  var list = listname;
  var objTitle=document.getElementsByName(‘txtTitle’)[0];
  var objName=document.getElementsByName(‘txtName’)[0];
  var batch = “<Batch onError=’Continue’ PreCalc=’TRUE’ ListVersion=’0′>”
  + ” <Method ID=’1′ Cmd=’New’>”
  + ” <Field Name=’ID’>New</Field>”
  + ” <Field Name=’Title’>”+objTitle.value+”</Field>”
  + ” <Field Name=’Name’>”+objName.value+”</Field>”
  + ” </Method>”
  + “</Batch>”

  var request = “<?xml version=’1.0′ encoding=’utf-8′?>”
  + “<soap:Envelope xmlns:xsi=’'&#8221;
  + ”      xmlns:xsd=’'&#8221;
  + ”      xmlns:soap=’’>&#8221;
  + ” <soap:Body>”
  + ” <UpdateListItems xmlns=’’>&#8221;
  + ”       <listName>” + list + “</listName>”
  + ”       <updates>” + batch + “</updates>”
  + ” </UpdateListItems>”
  + ” </soap:Body>”
  + “</soap:Envelope>”
//  var ws = new ActiveXObject(“Msxml2.XMLHTTP.4.0”);

  var ws = new ActiveXObject(“Microsoft.XMLHTTP”);“POST”,url,false);
  ws.setRequestHeader(“Content-Type”, “text/xml; charset=utf-8”);
alert(“Data saved successfully!”);



   First Name :  
   <input type=”text” name=”txtTitle”>
   Last Name :
   <input type=”text” name=”txtName”>
  <td colspan=”2″>
   <input type=button onClick=”CreateListItem(‘TempList’)” value=”Create List Item”>


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s