OBJECTIVE
To show different ways to retrieve lists:1) RETRIVING CURRENT LIST
2) RETRIVING LIST BY ‘TITLE’
3) RETRIVING LIST BY ‘ID’
4) RETRIVING ALL LISTS
5) RETRIVING A SINGLE PROPERTY FROM LISTS
1) RETRIVING CURRENT LIST
When you are working with lists (ex by Ribbon Button), sometime it is necessary to know the current list by client side.
If you are working with the standard view, you can use it:
var list = SP.ListOperation.Selection.getSelectedList();
Instead if you are working in datasheet, you may have some problems. At the following link it is suggested a workaround http://msdn.microsoft.com/en-us/library/ff410971.aspx.
2) RETRIVING LIST BY ‘TITLE’
In this example it retrieves one list by Title from the Root Web.<script type="text/ecmascript">
function initialize() {
var clientContext = new SP.ClientContext();var siteColl = clientContext.get_site();
myweb = siteColl.get_rootWeb();
this.list = myweb.get_lists().getByTitle('ListTitle'); //Edit the title
clientContext.load(list);
clientContext.executeQueryAsync(Function.createDelegate(this, GetList), Function.createDelegate(this, getFailed));
}
function GetList() {
alert(list.get_title() + ': ' + list.get_id().toString());}
function getFailed() {
alert('Failed.');
}
</script>
3) RETRIVING LIST BY ‘ID’
In this example it retrieves one list by Id from the Root Web.<script type="text/ecmascript">
SP.SOD.executeOrDelayUntilScriptLoaded(initialize, 'SP.js');
function initialize() {
var value = SP.ListOperation.Selection.getSelectedList();
var clientContext = new SP.ClientContext();
var siteColl = clientContext.get_site();
this.myweb = siteColl.get_rootWeb();
this.list = myweb.get_lists().getById('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'); //Edit the ID
clientContext.load(list);
clientContext.executeQueryAsync(Function.createDelegate(this, GetList), Function.createDelegate(this, getFailed));
}
function GetList() {
alert(list.get_title() + ': ' + list.get_id().toString());
}
function getFailed() {
alert('Failed.');
}
</script>
4) RETRIVING ALL LISTS
In this example it retrieves all the lists in the Root Web<script type="text/ecmascript">
SP.SOD.executeOrDelayUntilScriptLoaded(initialize, 'SP.js');
function initialize() {
var clientContext = new SP.ClientContext();var siteColl = clientContext.get_site();
myweb = siteColl.get_rootWeb();
this.lists = myweb.get_lists();
clientContext.load(lists);
clientContext.executeQueryAsync(Function.createDelegate(this, GetLists), Function.createDelegate(this, getFailed));
}
function GetLists() {
var listEnumerator = lists.getEnumerator();while (listEnumerator.moveNext()) {
list = listEnumerator.get_current();
alert(list.get_title());
}
}
function getFailed() {
alert('Failed.');
}
</script>
5) RETRIVING A SINGLE PROPERTY FROM LISTS
When you retrieve a list or a collection of list, you can choice what properties you want. In this example it retrieves the ID for all the lists in the root web.
<script type="text/ecmascript">SP.SOD.executeOrDelayUntilScriptLoaded(initialize, 'SP.js');
function initialize() {
var clientContext = new SP.ClientContext();
var siteColl = clientContext.get_site();
myweb = siteColl.get_rootWeb();
this.lists = myweb.get_lists();
clientContext.load(lists, 'Include(Id)');
clientContext.executeQueryAsync(Function.createDelegate(this, GetLists), Function.createDelegate(this, getFailed));
}
function GetLists() {
var listEnumerator = lists.getEnumerator();
while (listEnumerator.moveNext()) {
list = listEnumerator.get_current();
alert(list.get_id());
}
}
function getFailed() {
alert('Failed.');
}
</script>