Sunday, November 24, 2013

Get List data using JavaScript Client Object Model in SharePoint 2010

Here I am giving an example to get list data using java script client object model.

var listItems ;

function LoadEmployeeListData(empname) {

        var clientContext = new SP.ClientContext('/');
        var web = clientContext.get_web();
        var list = web.get_lists().getByTitle('EmployeeMaster');

        var query = new SP.CamlQuery();
        var queryString = '<View><Query><Where><Eq><FieldRef Name="EmpName"/><Value Type="Text">' + empname + '</Value></Eq></Where></Query></View>';
        listItems = list.getItems(query);

       clientContext.executeQueryAsync(Function.createDelegate(this, onSuccess), Function.createDelegate(this, onFail));

    function onSuccess() {
        // Do something useful like loop through your returned list items and output them somewhere
        // create an enumerator to loop through the list with
        var listItemEnumerator = this.listItems.getEnumerator();
        // loop through the list items
       while (listItemEnumerator.moveNext())
       var oListItem = listItemEnumerator.get_current();
document.getElementById('<%= txtEmpName.ClientID %>').value = oListItem.get_item('EmpName');
document.getElementById('<%= txtEmpCode.ClientID %>').value = oListItem.get_item('EmpCode');
document.getElementById('<%= txtEmpLocation.ClientID %>').value = oListItem.get_item('Location');
document.getElementById('<%= txtEmpDesig.ClientID %>').value = oListItem.get_item('Designation');
 document.getElementById('<%= txtEmpDept.ClientID %>').value = oListItem.get_item('Department');


 function onFail(sender, args)
        alert("failed. Message:" + args.get_message());


No comments:

Post a Comment