.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Sharepoint:Add list item Custom list: ECMA Script Client Object Model.

Posted By:Sagar Pardeshi       Posted Date: November 11, 2013    Points: 40    Category: SharePoint    URL: http://www.dotnetspark.com  

We have Add item in sharepoint Custom List using ECMA Script Client Object Model.
 

<Sharepoint:ScriptLink name="SP.js" runat="server" OnDemand="true" localizable="false" />


<input type="text" id="txtiem" />
<input type="button" id="btnitem" onclick="AddItem();" value="Add New Item" />
<label id ="lbl" runat="server"/>

<script type="text/ecmascript">
function AddItem() {
var txtItem = document.getElementById("txtiem");
if (txtItem.value == "") {
alert("please enter");
return;
}
//Retrieve the clientContext
var clientContext = SP.ClientContext.get_current();
//Retrieve the current website
var webSite = clientContext.get_web();
//Retrieve the lists
var lists = webSite.get_lists();
//Get the List
var carsList = lists.getByTitle("Lststmay");
//Create new IteamCreationInformation
var itemCreationInfo = new SP.ListItemCreationInformation();
//the addItem takes the itemCreationInformation object
//and returns the listItem
var listItem = carsList.addItem(itemCreationInfo);
//Set the title of the list Item
//to the value in the text
listItem.set_item("Title", txtItem.value);
listItem.set_item("Name", "Xyz");
//Call Update the listItem
listItem.update();
//Finally execute the operation
//pass to it a 2 callBack methods OnSucceed and OnFailure
clientContext.executeQueryAsync(
Function.createDelegate(this,this.Succeeded)
,Function.createDelegate(this,this.Failed));
}


function Succeeded() {
alert("Succeeded");
location.href="/sites/test/Lists/Lststmay/AllItems.aspx";
}
function Failed(sender,args) {
alert("fail");
}


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend