Retrieve items from list using JSOM

Updated: Sep 15, 2020

This blog demonstrates how to connect to a list and retrieve specific items based on filter/query using JSOM.

var ListItem;

function getTheList(employee) {

	clientContext = new SP.ClientContext.get_current();

	//get web object from current context
	var web = clientContext.get_web();

	//Provide list name and fetch the list
	var myList = web.get_lists().getByTitle('LIST_NAME');

	//write a query to fetch records based on filter
	var query = new SP.CamlQuery();
	query.set_viewXml('' + employee + '');

	//fetch list items based on query formed in above statement
	ListItem = myList.getItems(query);

	//Load the object
	clientContext.load(ListItem,'Include(EmployeeName)');

	//Final step to execute the query to get details for loaded objects
	clientContext.executeQueryAsync(Function.createDelegate(this, this.mySuccessFunction),
	Function.createDelegate(this, this.myFailFunction));

	return false;
}

//This method will be called on success of executeQueryAsync
function mySuccessFunction(sender, args)
{
	var listItemEnumerator = ListItem.getEnumerator();
	while (listItemEnumerator.moveNext())
	{
		var item = listItemEnumerator.get_current();

		var employee = item.get_item('EmployeeName');
		//Your code goes here..
	}
}

//This method will be called on failure or on any exception thrown in executeQueryAsync
function myFailFunction(sender, args)
{
	alert(args.get_message() + "\n" + args.get_stackTrace());
}
0 views0 comments

Recent Posts

See All

In this blog we will see how to export a list in excel using server side object model. First block we will retrieve all items from the list. This can be done in several ways. You can get all items wit