Nigel Price
Nigel Price

Reputation: 133

SharePoint 2013 In allitems.aspx How Do I get the list title and Name

I am trying to mimic the Alert Me functionality as the business only wants Alert Me and Send a link. In Alert Me I am trying to get the current list's Name and Title when my javascript code is in the allitems.aspx page of my document library. All of the examples I can find assume you know the title of the list already.

Upvotes: 0

Views: 2939

Answers (1)

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59328

At least the following options could be used to determine list properties (like Title) in List View page (AllItems.aspx)

Using SP.ListOperation.Selection namespace

SP.ListOperation.Selection.getSelectedList() Method gets the ID of the list being selected:

var listId = SP.ListOperation.Selection.getSelectedList(); 

The following example demonstrates how to retrieve list by its Id via CSOM (JavaScript):

(function(){

  var listId = SP.ListOperation.Selection.getSelectedList(); //selected List Id
  var context = new SP.ClientContext.get_current();
  var web = context.get_web();
  var list = web.get_lists().getById(listId);

  context.load(list);
  context.executeQueryAsync(
     function() {
       //print List properties
       console.log(list.get_title());
     },
     function(sender,args){
        console.log(args.get_message());
     }
  );
})();

Using _spPageContextInfo structure

_spPageContextInfo object is rendered in every SharePoint page and contains property _spPageContextInfo.pageListId that stores the current List Id:

var listId = _spPageContextInfo.pageListId;

Upvotes: 1

Related Questions