Ads

Tuesday, 2 April 2013

Ecma to GetListItem and fill in dropdown

<script type=”text/ecmascript” language=”ecmascript”>

ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js");
var ItemContainer = { ItemList: [] };

function retrieveListItems() {

var clientContext = SP.ClientContext.get_current();
// Get the SharePoint list by giving the display name of the list.
var oList = clientContext.get_web().get_lists().getByTitle(‘Categories’);
// To retreive all items use the predefined createAllItemsQuery operation.
var camlQuery = SP.CamlQuery.createAllItemsQuery();
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onListDataLoadQuerySucceeded),
Function.createDelegate(this, this.onListDataLoadQueryFailed));
}



// Callback function if the item retrieval Async call get successful.
function onListDataLoadQuerySucceeded(sender, args) {
var listItemInfo = ”;
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
// Fill a json object with Id and Value properties.
var tempItem = { Id: oListItem.get_id(), Value: oListItem.get_item(‘Title’) };
ItemContainer.ItemList.push(tempItem);
}
// Fill the drop down with retrieved data.
fillDropDown();
}
// Callback function if item retrieval failes.
function onListDataLoadQueryFailed(sender, args) {
alert(‘Request failed. ‘ + args.get_message() + ‘\n’ + args.get_stackTrace());
}


// Fill the drop down with the retrieved list item data.
function fillDropDown() {
var ddlCategory = document.getElementById(‘ddlCategory’);
if (ddlCategory != null) {
for (var i = 0; i < ItemContainer.ItemList.length; i++) {
var theOption = new Option;
theOption.value = ItemContainer.ItemList[i].Id;
theOption.text = ItemContainer.ItemList[i].Value;
ddlCategory.options[i] = theOption;
}
}
}


</script>

No comments:

Post a Comment

Ads