Sharepoint 2010:ECMA script to retrieve list data
Sharepoint 2010 has got new greate feature called "Client object Model" which is very useful.
Client object model helps developers in making things easier to develop client side applications in sharepoint 2010.
Using client object model we can do many of things with out writing server side code.
you will find enough of information on msdn about Client Object model and differerent platforms where it can be used,so iam will concentrating on ECMA script.
ECMA script:have you ever thought of using sharepoint objects in client(javascript) and updating the list items without having to write server code and with out having to deploy it as feature?
Yes its possible and here where ECMA script comes in to picture.
In this blog i am going to write about retrieving the list data using ECMA script.
Please find the code below to retrieve list data using ECMA script:
<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(getList1Details, "sp.js");
var clientContext= null;
function getList1Details()
{
var List1Items;
clientContext= new SP.ClientContext.get_current();
var ListName = clientContext.get_web().get_lists().getByTitle('List1');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml("<Query><Where><IsNotNull><FieldRef Name='Title' /></IsNotNull></Where></Query>");
List1Items =ListName.getItems(camlQuery);
clientContext.load(List1Items);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethodItems), Function.createDelegate(this, this.onFailureMethodItems));
}
function onSuccessMethodItems(sender, args)
{
var oListItemEnum=this.List1Items.getEnumerator();
while (oListItemEnum.moveNext()) //loop through items in list
{
var oListItem=oListItemEnum.get_current();
var strName=oListItem.get_item('EmpName'); //get 'EmpName' column value
alert('Employee Name:' + strName);
}
}
function onFailureMethodItems(sender, args)
{
alert('request failed for getting Items' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
so above code returns the data from a list called 'List1'.
you can further customize code as per your requirement.
Client object model helps developers in making things easier to develop client side applications in sharepoint 2010.
Using client object model we can do many of things with out writing server side code.
you will find enough of information on msdn about Client Object model and differerent platforms where it can be used,so iam will concentrating on ECMA script.
ECMA script:have you ever thought of using sharepoint objects in client(javascript) and updating the list items without having to write server code and with out having to deploy it as feature?
Yes its possible and here where ECMA script comes in to picture.
In this blog i am going to write about retrieving the list data using ECMA script.
Please find the code below to retrieve list data using ECMA script:
<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(getList1Details, "sp.js");
var clientContext= null;
function getList1Details()
{
var List1Items;
clientContext= new SP.ClientContext.get_current();
var ListName = clientContext.get_web().get_lists().getByTitle('List1');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml("<Query><Where><IsNotNull><FieldRef Name='Title' /></IsNotNull></Where></Query>");
List1Items =ListName.getItems(camlQuery);
clientContext.load(List1Items);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethodItems), Function.createDelegate(this, this.onFailureMethodItems));
}
function onSuccessMethodItems(sender, args)
{
var oListItemEnum=this.List1Items.getEnumerator();
while (oListItemEnum.moveNext()) //loop through items in list
{
var oListItem=oListItemEnum.get_current();
var strName=oListItem.get_item('EmpName'); //get 'EmpName' column value
alert('Employee Name:' + strName);
}
}
function onFailureMethodItems(sender, args)
{
alert('request failed for getting Items' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
so above code returns the data from a list called 'List1'.
you can further customize code as per your requirement.
Hi Chandrika,
ReplyDeleteI was trying to update multiple list items of a single list in the Success delegate.
However, I found that only the last one's update would get saved.
I tried updating the web, list objects inside the success handler.
Please let me know if you have a code sample or a link.
Thanks for any help here.
Rahul Vartak
http://rahul-vartak.blogspot.com/
Hi Rahul,
ReplyDeleteHere is the sample code to update list item,loop through the list using Enumerator as shown in the above blog code to update multiple list items.
var clientContext = null;
var web = null;
ExecuteOrDelayUntilScriptLoaded(UpdateListItem, "sp.js");
function UpdateListItem()
{
clientContext = new SP.ClientContext.get_current();
web = clientContext.get_web();
this.list = web.get_lists().getByTitle('Mylist');
this.oListItem = list.getItemById(1);
oListItem.set_item('Title', 'MYItem1');
oListItem.update();
clientContext.executeQueryAsync(Function.createDelegate(this, this.onUpdateListItemSuccess), Function.createDelegate(this, this.onQueryFailed));
}
function onUpdateListItemSuccess(sender, args) {
alert("list item updated");
}
function onQueryFailed(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}
Hi How to retreive only one item. becasue defaultly it return all item. more than 1000 item it will create performance problem?
ReplyDeleteHi Suresh,
DeletePlease use list.getItemById() and pass the Id to the method to get the specific list item.
Hi i am bhupesh working as a sharepoint developer.
ReplyDeletewe need to fetch out the web analytics data using ecma script?
Any help???