Create/Insert SharePoint List Items using ECMA Script (JSOM/Client Side Object Model)
Insert SharePoint list items in simple steps using Client Side Object Model.
Step 1: Create a SharePoint list, for eg: i have taken a list name "Cars" and i have created two fields "Car Name" and "Price" both are free text fields.
Step 2: Now we have to create a front end for the user to create list items, to do this we need to have knowledge on HTML. Here I have written my HTML code in the content editor web part, I have created two HTML Textboxes, one button for submitting the item to SharePoint list.
<table>
<tr><td>Car Name</td><td><input type="text" id="txtCar" /></td></tr>
<tr><td>Price</td><td><input type="text" id="price" /></td></tr>
<tr><td colspan="2"><input type="button" id="btnAddCar" onclick="AddCar();" value="Submit" /></td></tr>
</table>
Step 3: I have embedded the Step 2 HTML code and the Javascritpt code in the content editor. You can find the complete code below. Replace the bolded letters in the below code with name of your internal field names.
<Sharepoint:ScriptLink name="SP.js" runat="server" OnDemand="true" localizable="false" />
<script type = "text/javascript">
function AddCar() {
var txtCar = document.getElementById("txtCar");
var cost = document.getElementById("price");
if (txtCar.value == "") {
alert("please enter a car");
return;
}
var clientContext = SP.ClientContext.get_current();
var webSite = clientContext.get_web();
var lists = webSite.get_lists();
var carsList = lists.getByTitle("Cars"); // Here "Cars" is my list name
var itemCreationInfo = new SP.ListItemCreationInformation();
var listItem = carsList.addItem(itemCreationInfo);
listItem.set_item("InternalFieldName", txtCar.value); //specify the internal field name
listItem.set_item("InternalFieldName", price.value); // specify the internal field name
listItem.update(); //updating the item into the SP list
clientContext.executeQueryAsync(
Function.createDelegate(this,this.Succeeded)
,Function.createDelegate(this,this.Failed));
resetVal(); // calling function resetVal()
}
function resetVal()
{
document.getElementById("txtCar").value = ""; //Clear textbox after submit the item
document.getElementById("price").value = ""; //Clear textbox after submit the item
}
function Succeeded(sender,args) {
alert("Succeeded"); // Success message, after submit the item.
}
function Failed(sender,args) {
alert("fail"); //Failure message, after submit the item.
}
</script>
<table>
<tr><td>Car Name</td><td><input type="text" id="txtCar" /></td></tr>
<tr><td>Price</td><td><input type="text" id="price" /></td></tr>
<tr><td colspan="2"><input type="button" id="btnAddCar" onclick="AddCar();" value="Submit" /></td></tr>
<tr><td colspan="2"><label id ="lbl" runat="server"/></td>
</table>
Hope it helps :-)
Step 1: Create a SharePoint list, for eg: i have taken a list name "Cars" and i have created two fields "Car Name" and "Price" both are free text fields.
Step 2: Now we have to create a front end for the user to create list items, to do this we need to have knowledge on HTML. Here I have written my HTML code in the content editor web part, I have created two HTML Textboxes, one button for submitting the item to SharePoint list.
<table>
<tr><td>Car Name</td><td><input type="text" id="txtCar" /></td></tr>
<tr><td>Price</td><td><input type="text" id="price" /></td></tr>
<tr><td colspan="2"><input type="button" id="btnAddCar" onclick="AddCar();" value="Submit" /></td></tr>
</table>
Step 3: I have embedded the Step 2 HTML code and the Javascritpt code in the content editor. You can find the complete code below. Replace the bolded letters in the below code with name of your internal field names.
<Sharepoint:ScriptLink name="SP.js" runat="server" OnDemand="true" localizable="false" />
<script type = "text/javascript">
function AddCar() {
var txtCar = document.getElementById("txtCar");
var cost = document.getElementById("price");
if (txtCar.value == "") {
alert("please enter a car");
return;
}
var clientContext = SP.ClientContext.get_current();
var webSite = clientContext.get_web();
var lists = webSite.get_lists();
var carsList = lists.getByTitle("Cars"); // Here "Cars" is my list name
var itemCreationInfo = new SP.ListItemCreationInformation();
var listItem = carsList.addItem(itemCreationInfo);
listItem.set_item("InternalFieldName", txtCar.value); //specify the internal field name
listItem.set_item("InternalFieldName", price.value); // specify the internal field name
listItem.update(); //updating the item into the SP list
clientContext.executeQueryAsync(
Function.createDelegate(this,this.Succeeded)
,Function.createDelegate(this,this.Failed));
resetVal(); // calling function resetVal()
}
function resetVal()
{
document.getElementById("txtCar").value = ""; //Clear textbox after submit the item
document.getElementById("price").value = ""; //Clear textbox after submit the item
}
function Succeeded(sender,args) {
alert("Succeeded"); // Success message, after submit the item.
}
function Failed(sender,args) {
alert("fail"); //Failure message, after submit the item.
}
</script>
<table>
<tr><td>Car Name</td><td><input type="text" id="txtCar" /></td></tr>
<tr><td>Price</td><td><input type="text" id="price" /></td></tr>
<tr><td colspan="2"><input type="button" id="btnAddCar" onclick="AddCar();" value="Submit" /></td></tr>
<tr><td colspan="2"><label id ="lbl" runat="server"/></td>
</table>
Hope it helps :-)
Subscribe to:
Posts
(
Atom
)
No comments :
Post a Comment