In basic Sharepoint object model we can get title of current list (list which is currently opened in browser) by calling SPContext.Current.List.Title. However in Sharepoint Online we need to use javascript object model where the same task is done little bit more tricky. Here is the code:
1: SP.SOD.executeFunc("sp.js", "SP.ClientContext", function () {
2: SP.SOD.executeFunc("sp.core.js", "SP.ListOperation.Selection",
3: function () {
4: var listId = SP.ListOperation.Selection.getSelectedList();
5: if (typeof (listId) == "undefined" || listId == null || listId == "") {
6: return;
7: }
8: var ctx = SP.ClientContext.get_current();
9: var list = ctx.get_web().get_lists().getById(listId);
10: ctx.load(list);
11: ctx.executeQueryAsync(
12: Function.createDelegate(this, function (sender, args) {
13: console.log(list.get_title());
14: }),
15: Function.createDelegate(this, function (sender, args) {
16: console.log(args.get_message());
17: })
18: );
19: });
20: });
I.e. at first we get id of the current list by calling SP.ListOperation.Selection.getSelectedList() (line 4) and then get list instance by this id and load it from context (lines 8-18). In success handler list.get_title() is available.
No comments:
Post a Comment