Get List,ListItems using Ecmascript\Javascript Client Object model SharePoint 2010

Follow Us :

To Retrieve Sites\Subsites and Site collections See the Get Site,Site collection using Ecmascript\Javascript Client Object model SharePoint 2010

Get List  –

<script type="text/javascript">

var currentcontext = null;
var currentweb = null;

ExecuteOrDelayUntilScriptLoaded(GetList, "sp.js");

function GetList()
{

currentcontext = new SP.ClientContext.get_current();

currentweb = currentcontext.get_web();

this.list = currentweb.get_lists().getByTitle("ListName");

currentcontext.load(list, 'Title', 'Id');

currentcontext.executeQueryAsync(Function.createDelegate(this, this.ExecuteOnSuccess),
Function.createDelegate(this, this.ExecuteOnFailure));

}

function ExecuteOnSuccess(sender, args) {

alert('List title : ' + this.list.get_title() + '; List ID : '+ this.list.get_id());

//alert('success');

}

function ExecuteOnFailure(sender, args) {
alert("Error in Getting List ID");
}
</script>

Get List Items -

<script type="text/ecmascript">
ExecuteOrDelayUntilScriptLoaded(GetItems,  "sp.js");
function GetItems()
{
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle('Testlist');
var query = SP.CamlQuery.createAllItemsQuery();
allItems = list.getItems(query);
context.load(allItems, 'Include(Title,Department)');
context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed));
}
function success() {
var TextFiled = “”;
var ListEnumerator = this.allItems.getEnumerator();
while(ListEnumerator.moveNext())
{
var currentItem = ListEnumerator.get_current();
TextFiled  += currentItem.get_item('Title') + '-' +currentItem.get_item('Department') + '\n';
}
alert(TextFiled);
}
function failed(sender, args) {
alert("failed. Message:" + args.get_message());
}
</script>



Tags: SharePoint 2010